0

Keras 모델 API를 사용하여 UNet을 다시 만들려고하고 있는데, 셀 이미지와 세그먼트 버전을 수집했으며이를 사용하여 모델을 교육하려고합니다. 그렇게함으로써 다른 셀을 업로드하고 분할 된 버전의 이미지를 예측으로 얻을 수있었습니다. Keras 모델 - Unet Image Segmentation

from __future__ import print_function 

from matplotlib import pyplot as plt 
from keras import losses 
import os 
from keras.models import Model 
from keras.layers import Input, concatenate, Conv2D, MaxPooling2D, Conv2DTranspose 
from keras.optimizers import Adam 
import cv2 
import numpy as np 
# training data 
image_location = "C:/Users/JamilG-Lenovo/Desktop/train/" 
image = image_location+"image" 
label = image_location +"label" 


class train_data(): 

    def __init__(self, image, label): 
     self.image = [] 
     self.label = [] 
     for file in os.listdir(image): 
      if file.endswith(".tif"): 
       self.image.append(cv2.imread(image+"/"+file,0)) 

     for file in os.listdir(label): 
      if file.endswith(".tif"): 
       #print(label+"/"+file) 
       self.label.append(cv2.imread(label+"/"+file,0)) 

    def get_image(self): 
     return np.array(self.image) 

    def get_label(self): 
     return np.array(self.label) 



def get_unet(rows, cols): 
    inputs = Input((rows, cols, 1)) 
    conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs) 
    conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv1) 
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) 

    conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(pool1) 
    conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv2) 
    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) 

    conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool2) 
    conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv3) 
    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3) 

    conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(pool3) 
    conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv4) 
    pool4 = MaxPooling2D(pool_size=(2, 2))(conv4) 

    conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(pool4) 
    conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv5) 

    up6 = concatenate([Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(conv5), conv4], axis=3) 
    conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(up6) 
    conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv6) 

    up7 = concatenate([Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(conv6), conv3], axis=3) 
    conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(up7) 
    conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv7) 

    up8 = concatenate([Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(conv7), conv2], axis=3) 
    conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(up8) 
    conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv8) 

    up9 = concatenate([Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same')(conv8), conv1], axis=3) 
    conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(up9) 
    conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv9) 

    conv10 = Conv2D(1, (1, 1), activation='sigmoid')(conv9) 

    model = Model(inputs=[inputs], outputs=[conv10]) 
    model.compile(optimizer=Adam(lr=1e-5), loss = losses.mean_squared_error) 

    return model 



def main(): 
    # load all the training images 
    train_set = train_data(image, label) 
    # get the training image 
    train_images = train_set.get_image() 
    # get the segmented image 
    train_label = train_set.get_label() 
    print("type of train_images" + str(type(train_images[0]))) 
    print("type of train_label" + str(type(train_label[0]))) 
    print('\n') 
    print("shape of train_images" + str(train_images[0].shape)) 
    print("shape of train_label" + str(train_label[0].shape)) 


    plt.imshow(train_images[0], interpolation='nearest') 
    plt.title("actual image") 
    plt.show() 

    plt.imshow(train_label[0], interpolation='nearest') 
    plt.title("segmented image") 
    plt.show() 
    # create a UNet (512,512) 
    unet = get_unet(train_label[0].shape[0], 
        train_label[0].shape[1]) 

    # look at the summary of the unet 
    unet.summary() 
    #-----------errors start here----------------- 

    # fit the unet with the actual image, train_images 
    # and the output, train_label 
    unet.fit(train_images, train_label, batch_size=16, epochs=10) 

main() 

https://github.com/JamilGafur/Unet

나는 그것이 10 시대에 걸쳐 피팅 것으로 예상, 대신, 그것은 다음과 같은 오류 던지고 그것을 실행하려고 오전 :

File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", 
line 144, in _standardize_input_data str(array.shape)) 

ValueError: Error when checking input: expected input_5 to have shape (None, 
512, 512, 1) but got array with shape (1, 30, 512, 512) 

누군가 내가 뭘 잘못했는지, 코드가 무엇인지, 올바른 방향으로 나를 가리키면, 나는 많은 감사를 표한다. 그것.

감사합니다.

답변

0

"채널 첫 번째 모드"에서 이미지를 전달하는 동안 Keras가 "마지막 채널"을 기다리고 있다고 생각합니다.

이 설정을 변경하는 방법은 다음과 같습니다. https://keras.io/backend/