Esempio n. 1
0
def train_unet_mobilenetv2(saveModelFn, tensorboardPath):
    # train_imgDir = "/home/xiping/mydisk2/imglib/my_imglib/coco/train2014_person"
    train_imgDir = "/coco/train2014_person"
    (train_data, train_mask_data), (val_data,
                                    val_mask_data) = get_data(train_imgDir,
                                                              maxNum=12000,
                                                              valMaxNum=1000)

    # print(train_data.shape)
    # print(mask_data.shape)
    # print(mask_data[0])
    # cv2.imwrite("xx.bmp", mask_data[1]*255)
    # exit(0)

    print("================================")
    BACKBONE = 'mobilenetv2'
    # define model
    model = Unet(
        BACKBONE,
        classes=1,
        input_shape=(224, 224,
                     3),  # specific inputsize for callback save model
        activation='sigmoid',  #sigmoid,softmax
        encoder_weights='imagenet')

    # Show network structure.
    # model.summary()

    model.compile('Adam', loss='jaccard_loss', metrics=['iou_score'])
    # model.compile('SGD', loss="bce_dice_loss", metrics=["dice_score"])
    # model.compile('SGD', loss="bce_jaccard_loss", metrics=["iou_score"])
    # model.compile('adam', loss="binary_crossentropy", metrics=["iou_score"])

    checkpointer = ModelCheckpoint(
        filepath=
        "weights.epoch={epoch:02d}-val_loss={val_loss:.2f}-val_iou_score={val_iou_score:.2f}.hdf5",
        verbose=1,
        save_best_only=True)

    print("================================")
    print("Start train...")
    # fit model
    # if you use data generator use model.fit_generator(...) instead of model.fit(...)
    # more about `fit_generator` here: https://keras.io/models/sequential/#fit_generator
    model.fit(
        x=train_data,
        y=train_mask_data,
        batch_size=32,
        epochs=200,
        validation_data=(
            val_data,
            val_mask_data),  # callback save middle model need input val data
        callbacks=[TensorBoard(log_dir=tensorboardPath), checkpointer])

    model.save(saveModelFn)
def train():
    #load images
    images = []
    for image in os.listdir(im_path):
        imi = cv.imread(os.path.join(im_path, image))
        images.append(imi)

    #load masks
    masks = []
    for mask in os.listdir(mask_path):
        mask_in = cv.imread(os.path.join(mask_path, mask), 0)
        ret_val, threshed_mask = cv.threshold(mask_in, 37, 1, cv.THRESH_BINARY)
        masks.append(threshed_mask)

    model = Unet('resnet34',
                 encoder_weights='imagenet',
                 input_shape=(128, 128, 3))
    model.compile('Adam',
                  loss=bce_jaccard_loss,
                  metrics=[iou_score, 'accuracy'])
    model.summary()
    hist = model.fit(x=np.array(images).reshape(-1, 128, 128, 3),
                     y=np.array(masks).reshape(-1, 128, 128, 1),
                     batch_size=10,
                     epochs=15)

    #save model
    filename = 'trained_model.h5'
    model.save(filename, include_optimizer=False)
Esempio n. 3
0
def train(x_train: NpArray, x_valid: NpArray, y_train: NpArray, y_valid: NpArray,
          fold: int = -1) -> None:
    preprocessing_fn = get_preprocessing('resnet34')
    x_train = preprocessing_fn(x_train)
    x_valid = preprocessing_fn(x_valid)

    model = Unet(backbone_name='resnet34', encoder_weights='imagenet')
    model.compile('Adam', 'binary_crossentropy', metrics=[my_iou_metric])
    model.summary()

    model_name = make_output_path("models/fold%d.hdf5" % fold)
    model_checkpoint = ModelCheckpoint(model_name, monitor='val_my_iou_metric',
                                       mode='max', save_best_only=True, verbose=1)
    reduce_lr = ReduceLROnPlateau(monitor='val_my_iou_metric', mode='max',
                                  factor=0.5, patience=5, min_lr=3e-6, verbose=1)

    model.fit(x_train, y_train, validation_data=[x_valid, y_valid], epochs=EPOCHS,
              batch_size=BATCH_SIZE, callbacks=[model_checkpoint, reduce_lr],
              verbose=VERBOSE)
Esempio n. 4
0
def main():

    train_datagen = ImageDataGenerator(rescale=1 / 255)
    train_batches = train_datagen.flow_from_directory(DATASET_PATH,
                                                      target_size=(1024, 1024),
                                                      shuffle=True,
                                                      class_mode=None,
                                                      batch_size=BATCH_SIZE)

    valid_datagen = ImageDataGenerator(rescale=1 / 255)
    valid_batches = valid_datagen.flow_from_directory(DATASET_PATH,
                                                      target_size=(1024, 1024),
                                                      shuffle=False,
                                                      class_mode=None,
                                                      batch_size=BATCH_SIZE)

    train_crops = crop_generator(train_batches, CROP_LENGTH)  #224
    valid_crops = crop_generator(valid_batches, CROP_LENGTH)

    batch_x_random_crop, batch_y_targeted_crop = next(train_crops)
    valid_x, valid_y = next(valid_crops)

    in_painted_x = in_painting_mask(batch_x_random_crop, batch_y_targeted_crop)
    valid_in_x = in_painting_mask(valid_x, valid_y)

    batch_x_random_crop = rgb2gray(batch_x_random_crop)
    batch_x_random_crop = np.reshape(
        batch_x_random_crop, (batch_x_random_crop.shape[0], 224, 224, 1))

    valid_x = rgb2gray(valid_x)
    valid_x = np.reshape(valid_x, (valid_x.shape[0], 224, 224, 1))

    model = Unet(backbone_name='resnet18',
                 encoder_weights='imagenet',
                 decoder_block_type='transpose')  # build U-Net
    model.compile(optimizer='Adam', loss='mean_squared_error')
    model.summary()
    model.fit(x=in_painted_x,
              y=batch_x_random_crop,
              validation_data=(valid_in_x, valid_x),
              validation_steps=5,
              steps_per_epoch=5,
              epochs=1)
Esempio n. 5
0
def main():

    image_path = "image"  #加载训练图片
    im_start = 0  #确定编号
    im_end = 29
    im_array = load_png_files(image_path, im_start, im_end)
    im_array = im_array[:, :, :, np.newaxis]  # 需要加代表图片的通道数,这里是黑白图片所以是1,因此直接加一维
    print("train_image shape : " + im_array.shape)

    label_path = "label"  #加载训练图片对应的标签图片
    la_start = 0
    la_end = 29
    la_array = load_png_files(label_path, la_start, la_end)
    la_array = la_array[:, :, :, np.newaxis]
    print("train_label shape : " + la_array.shape)

    test_path = "test"  #加载测试集的图片
    te_start = 0
    te_end = 4
    te_array = load_png_files(test_path, te_start, te_end)
    te_array = te_array[:, :, :, np.newaxis]
    print("test_image shape : " + te_array.shape)

    model = Unet('resnet34', input_shape=(512, 512, 1),
                 encoder_weights=None)  #1代表通道数
    model.compile('Adam', loss='binary_crossentropy', metrics=['accuracy'])
    model.fit(
        x=im_array,
        y=la_array,
        batch_size=10,
        epochs=8,
        validation_split=0.2,  #取训练集中的0.2作为验证集
        shuffle=True)
    model.save("model_v1.h5")  #保存模型

    print("Saved model to disk")
    print("done!!!!!")
Esempio n. 6
0
def train_net():
    args = parser.parse_args()
    final_path = args.model_final_path
    checkpoint_path = args.model_checkpoint_path
    dataset_path = args.dataset_path
    with tf.device("/gpu:0"):
        backbone = 'resnet50'
        preprocess_input = get_preprocessing(backbone)

        # load your data
        x_train, y_train, x_val, y_val = from_directory_datagen(dataset_path)

        # preprocess input
        x_train = preprocess_input(x_train)
        x_val = preprocess_input(x_val)

        # define model
        model = Unet(backbone, encoder_weights='imagenet', input_shape=(256, 256, 3))
        model.compile(optimizer=tf.keras.optimizers.Adam(lr=1e-3), loss=dice_loss,
                      metrics=[f1_score, iou_score])

        check_point = [ModelCheckpoint(checkpoint_path + 'model-{epoch:03d}-{val_f1-score:03f}.h5', verbose=1,
                                       monitor='val_f1-score',
                                       save_best_only=True, mode='max')]

        # fit model
        model.fit(
            x=(pair for pair in zip(x_train, y_train)),
            epochs=10,
            steps_per_epoch=x_train.n // x_train.batch_size,
            validation_data=(pair for pair in zip(x_val, y_val)),
            validation_steps=x_val.n // x_val.batch_size,
            verbose=1,
            shuffle=True,
            callbacks=check_point,
        )
        model.save(final_path + 'final_model.h5')
Esempio n. 7
0
def unet_train():
    callbacks = [
        # EarlyStopping(patience=10, verbose=1),
        # ReduceLROnPlateau(factor=0.1, patience=3, min_lr=0.00001, verbose=1),
        ModelCheckpoint('unet_' + checkpoint,
                        verbose=1,
                        monitor='loss',
                        save_best_only=True,
                        save_weights_only=True)
    ]
    model = Unet(backbone_name=backbone,
                 encoder_weights=None,
                 input_shape=input_shape)
    model.compile('Adam', 'binary_crossentropy', ['binary_accuracy'])
    results = model.fit(X_train,
                        y_train,
                        callbacks=callbacks,
                        epochs=100,
                        verbose=1,
                        validation_data=(X_valid, y_valid))
    plotting(results)
Esempio n. 8
0
    img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)
    X_test[n] = img

print('Done importing images')

#Define IoU metric
def mean_iou(y_true, y_pred):
    prec = []
    for t in np.arange(0.5, 1.0, 0.05):
        y_pred_ = tf.to_int32(y_pred > t)
        score, up_opt = tf.metrics.mean_iou(y_true, y_pred_, 2)
        K.get_session().run(tf.local_variables_initializer())
        with tf.control_dependencies([up_opt]):
            score = tf.identity(score)
        prec.append(score)
    return K.mean(K.stack(prec), axis=0)

# Build U-Net model (transfer model version, must match model in transfer_test_loop.py)
logdir="logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard = TensorBoard(log_dir=logdir)
transfer_model = Unet(backbone_name=backbone, input_shape=(None,None,3), classes=1,
             activation='relu', encoder_weights='imagenet', encoder_freeze=True)
transfer_model.compile(optimizer='Adam', loss='binary_crossentropy', metrics=[mean_iou])
transfer_model.summary()
earlystopper = EarlyStopping(patience=patience, verbose=1)
checkpointer = ModelCheckpoint((WorkingDir + '/' + model_name), verbose=1, save_best_only=True)
results = transfer_model.fit(X_train, Y_train, validation_split=val_split, batch_size=batch, epochs=epochs, callbacks=[earlystopper, checkpointer, tensorboard])

# Evaluate how well training went in tensorboard
%load_ext tensorboard
%tensorboard --logdir logs --host=127.0.0.1
Esempio n. 9
0
check_point = keras.callbacks.ModelCheckpoint(
    os.path.join(model_path, config.exp_name + ".h5"),
    monitor='val_loss',
    verbose=1,
    save_best_only=True,
    mode='min',
)
callbacks = [check_point, early_stopping, tbCallBack]

while config.batch_size > 1:
    # To find a largest batch size that can be fit into GPU
    try:
        model.fit(x_train,
                  y_train,
                  batch_size=config.batch_size,
                  epochs=config.nb_epoch,
                  verbose=config.verbose,
                  shuffle=True,
                  validation_data=(x_valid, y_valid),
                  callbacks=callbacks)
        break
    except tf.errors.ResourceExhaustedError as e:
        config.batch_size = int(config.batch_size / 2.0)
        print("\n> Batch size = {}".format(config.batch_size))

if config.model == "Unet":
    model = Unet(backbone_name=config.backbone,
                 encoder_weights=config.weights,
                 decoder_block_type=config.decoder_block_type,
                 classes=config.nb_class,
                 activation=config.activation)
elif config.model == "Nestnet":
Esempio n. 10
0
# #model1.summary()


# In[20]:


#early_stopping = EarlyStopping(monitor='my_iou_metric', mode = 'max',patience=10, verbose=1)
model_checkpoint = ModelCheckpoint(save_model_name,monitor='my_iou_metric', 
                                   mode = 'max', save_best_only=True, verbose=1)
reduce_lr = ReduceLROnPlateau(monitor='my_iou_metric', mode = 'max',factor=0.5, patience=5, min_lr=0.0001, verbose=1)

epochs = 50
batch_size = 32
history = model1.fit(x_train, y_train,
                    validation_data=[x_valid, y_valid], 
                    epochs=epochs,
                    batch_size=batch_size,
                    callbacks=[ model_checkpoint,reduce_lr], 
                    verbose=2)


# In[21]:


model1 = load_model(save_model_name,custom_objects={'my_iou_metric': my_iou_metric})
# model1 = load_model(save_model_name,custom_objects={'my_iou_metric': my_iou_metric, 'lovasz_loss': lovasz_loss, 'my_iou_metric_2': my_iou_metric_2})
# remove layter activation layer and use losvasz loss
input_x = model1.layers[0].input

output_layer = model1.layers[-1].input
model = Model(input_x, output_layer)
c = optimizers.adam(0.0005)
Esempio n. 11
0
def scheduler(epoch):
    if epoch < 10:
        return 0.001
    else:
        return 0.001 * tf.math.exp(0.1 * (10 - epoch))


lr_schedule = tf.keras.callbacks.LearningRateScheduler(scheduler)

start_time = time.time()
# train model
model.fit(
    train_sequence_generator,
    steps_per_epoch=100,
    epochs=1000,
    verbose=1,
    callbacks=[earlystopper, tensorboard, lr_schedule],
    validation_data=validation_sequence_generator,
    validation_steps=5
)
end_time = time.time()

if not os.path.exists(f'../../reports/logs_and_plots/{test_name}/'):
    os.makedirs(f'../../reports/logs_and_plots/{test_name}/')

with open(f'../../reports/logs_and_plots/{test_name}/{test_name}_log.txt', 'w+') as file:
    file.write(f'Training completed in {end_time-start_time:0.1f} seconds.\n')

# save trained model
model.save(f'../../models/{test_name}.h5')
Esempio n. 12
0
def main():
    WEIGHTS_PATH = 'weights'

    logger.info("Reading images")
    for file in os.listdir(PATH_TO_IMAGES):
        if file.endswith(".tiff"):
            ##### /read an image in the dataset #####
            img_path = os.path.join(PATH_TO_IMAGES, file)

            img = PIL.Image.open(img_path)
            img = img.convert('RGB')
            img = np.asarray(img) #[H, W, C]
            img = utils.normalize(img)
            ##### /read an image in the dataset #####

            ##### read a label in the dataset #####
            f_name, f_ext = os.path.splitext(file)
            label_path = os.path.join(PATH_TO_LABELS, f_name + ".tif")
            assert os.path.isfile(label_path)
            label = PIL.Image.open(label_path)
            #label = label.convert('RGB')
            label = np.asarray(label) #[H, W, num_classes]
            ##### /read a label in the dataset #####
            
            train_xsz = int(3/4 * img.shape[0])  # use 75% of image as train and 25% for validation
            X_DICT_TRAIN[file] = img[:train_xsz, :, :]
            #print (X_DICT_TRAIN[file].shape)
            Y_DICT_TRAIN[file] = label[:train_xsz, :, :]
            #print (Y_DICT_TRAIN[file].shape)
            X_DICT_VALIDATION[file] = img[train_xsz:, :, :]
            Y_DICT_VALIDATION[file] = label[train_xsz:, :, :]

    logger.info("Training set: {} images".format(len(X_DICT_TRAIN)))
    logger.info("Training set: {} labels".format(len(Y_DICT_TRAIN)))
    logger.info("Validation set: {} images".format(len(X_DICT_VALIDATION)))
    logger.info("Validation set: {} labels".format(len(Y_DICT_VALIDATION)))

    x_train, y_train = utils.get_patches(X_DICT_TRAIN, Y_DICT_TRAIN, n_patches=TRAIN_SZ, sz=PATCH_SZ)
    assert len(x_train) == len(y_train)
    logger.info("Generated {} patches for training".format(len(x_train)))

    x_val, y_val = utils.get_patches(X_DICT_VALIDATION, Y_DICT_VALIDATION, n_patches=VAL_SZ, sz=PATCH_SZ)
    assert len(x_val) == len(y_val)
    logger.info("Generated {} patches for validation".format(len(x_val)))


    logger.info("########## Training ##########")
    # define model
    model = Unet()
    model = Unet(backbone_name='resnet34', input_shape=(PATCH_SZ, PATCH_SZ, 3), classes=N_CLASSES, encoder_weights=None)
    
    model.compile('Adam', loss=bce_jaccard_loss, metrics=[iou_score])

    # load weights (if specified)
    if not os.path.exists(WEIGHTS_PATH):
        os.makedirs(WEIGHTS_PATH)
    WEIGHTS_PATH += "/seg_weights.{epoch:02d}-{val_iou_score:.2f}.hdf5"

    ########## define callbacks ##########
    model_checkpoint = keras.callbacks.ModelCheckpoint(
        WEIGHTS_PATH, 
        monitor='val_loss', 
        verbose=1, 
        save_best_only=True, 
        period=5
    )
    
    csv_logger = keras.callbacks.CSVLogger(
        'log_unet.csv', 
        append=True, 
        separator=';'
    )

    tensorboard = keras.callbacks.TensorBoard(
        log_dir='./tensorboard_unet/', 
        write_graph=True, 
        write_images=True
    )
    ########## /define callbacks ##########

    # fit model
    model.fit(
        x=x_train,
        y=y_train,
        batch_size=BATCH_SIZE,
        epochs=N_EPOCHS,
        verbose=1, #show an animated progress bar
        shuffle=True,
        callbacks=[
            model_checkpoint, 
            csv_logger, 
            tensorboard
        ],
        validation_data=(x_val, y_val),
    )
assert train_dataloader[0][0].shape == (BATCH_SIZE, 256, 256, 3)
assert train_dataloader[0][1].shape == (BATCH_SIZE, 256, 256, 21)

NAME = "Model_Unet"
# define callbacks for learning rate scheduling and best checkpoints saving
callbacks = [
    keras.callbacks.TensorBoard(log_dir='logs\{}'.format(NAME), update_freq='epoch'),
    keras.callbacks.ModelCheckpoint('./best_model_unet.h5', save_weights_only=False, save_best_only=True, \
                                    mode='max', monitor='val_iou_score'),
    keras.callbacks.ReduceLROnPlateau(monitor='val_loss', min_lr=0.00000001, patience=2),
    metrics
]

!rm - rf / logs

history_unet = model.fit(train_dataloader, steps_per_epoch=len(train_dataloader),
                         epochs=100, validation_data=test_dataloader, callbacks=callbacks)

# Plot training & validation iou_score values
plt.figure(figsize=(30, 5))
plt.subplot(121)
plt.plot(history_unet.history['iou_score'])
plt.plot(history_unet.history['val_iou_score'])
plt.title('Model iou_score')
plt.ylabel('iou_score')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')

# Plot training & validation loss values
plt.subplot(122)
plt.plot(history_unet.history['loss'])
plt.plot(history_unet.history['val_loss'])
Esempio n. 14
0
focal_loss = sm.losses.CategoricalFocalLoss()
total_loss = dice_loss + (1 * focal_loss)
metrics = [
    sm.metrics.IOUScore(threshold=0.5),
    sm.metrics.FScore(threshold=0.5)
]

# compile keras model with defined optimozer, loss and metrics
model.compile(optim, total_loss, metrics)

if False:
    print("training")
    history = model.fit(
        train_dataloader,
        steps_per_epoch=len(train_dataloader),
        epochs=EPOCHS,
        callbacks=callbacks,
        validation_data=validation_dataloader,
        validation_steps=len(validation_dataloader),
    )

    # Plot training & validation iou_score values
    plt.figure(figsize=(30, 5))
    plt.subplot(121)
    plt.plot(history.history['iou_score'])
    plt.plot(history.history['val_iou_score'])
    plt.title('Model iou_score')
    plt.ylabel('iou_score')
    plt.xlabel('Epoch')
    plt.legend(['Train', 'Test'], loc='upper left')

    # Plot training & validation loss values
Esempio n. 15
0
                                  min_lr=0.00001,
                                  verbose=1),
                ModelCheckpoint(comb[combinations] + "_" + BACKBONE + "_" +
                                name_model + "_temporary_model_IoU" +
                                str(size) + ".h5",
                                verbose=1,
                                save_best_only=True,
                                save_weights_only=True), time_callback
            ]

            ## fit model
            if name_model == "PSPNet":
                model.fit(x=x_train2,
                          y=y_train2,
                          batch_size=n_batch,
                          epochs=n_epochs,
                          class_weight=class_weight,
                          validation_data=(x_val2, y_val2),
                          callbacks=callbacks)
            else:
                model.fit(x=x_train,
                          y=y_train,
                          batch_size=n_batch,
                          epochs=n_epochs,
                          class_weight=class_weight,
                          validation_data=(x_val, y_val),
                          callbacks=callbacks)
            times = time_callback.times
            dic_times = {}
            dic_times['times'] = times
            savemat(
             encoder_weights = 'imagenet', 
             encoder_freeze = True)    # freezing weights as pre-trained weights are used

model.compile(optimizer, 
              loss = JaccardLoss(per_image = False), 
              metrics = ['categorical_accuracy', IOUScore(per_image = False, threshold = 0.5)])

# creating generators for the image augmentation
train_generator = backroom.UnetSequence(X_train, y_train_multidim, batch_size, augmentations = backroom.train_augmentation) 
test_generator = backroom.UnetSequence(X_test, y_test_multidim, batch_size, augmentations = None)


start_time = time.time() # measuring modelling time

# basic .fit method
model.fit(X_train, y_train_multidim, epochs = 2, batch_size = batch_size, validation_data = (X_test, y_test_multidim)) 

set_trainable(model, recompile = False) # Set all layers of model trainable, so that encode_freeze is lifted. Recompile = True does not work with Tensorflow 2.0
model.compile(optimizer, 
              loss = JaccardLoss(per_image = False), 
              metrics = ['categorical_accuracy', IOUScore(per_image = False, threshold = 0.5)])

# fit_generator method for image augmentation
model.fit_generator(train_generator, 
                    validation_data=test_generator, 
                    steps_per_epoch=len(X_train) // batch_size, 
                    validation_steps=len(X_test) // batch_size, 
                    epochs=epoch_no, 
                    callbacks=backroom.callbacks)

elapsed_time = time.time()-start_time # measuring modelling time
Esempio n. 17
0
                    "_temporary_model_IoU" + str(size) + ".h5",
                    verbose=1,
                    save_best_only=True,
                    save_weights_only=True), time_callback, tensorboard
            ]

            #            dataAugmentaion = image.ImageDataGenerator(rotation_range = 30, zoom_range = 0.20,
            #            fill_mode = "nearest", shear_range = 0.20, horizontal_flip = True,
            #            width_shift_range = 0.1, height_shift_range = 0.1)
            #            model.fit_generator(dataAugmentaion.flow(x_train, y_train, batch_size = n_batch), validation_data = (x_val, y_val), steps_per_epoch = len(x_train) // n_batch, epochs = n_epochs)

            model.fit(
                #                x=[np.reshape(x_train[:,:,:,:5],newshape=(y_train.shape[0],y_train.shape[1],y_train.shape[2],5)),np.reshape(x_train[:,:,:,5],newshape=(y_train.shape[0],y_train.shape[1],y_train.shape[2],1))], #reverse dimensions (in load_data_SR) and input from y_train
                x=x_train,
                y=y_train,
                batch_size=n_batch,
                epochs=n_epochs,
                #                class_weight = class_weight,
                #                validation_data=([np.reshape(x_val[:,:,:,:5],newshape=(x_val.shape[0],x_val.shape[1],x_val.shape[2],5)), np.reshape(x_val[:,:,:,5],newshape=(x_val.shape[0],x_val.shape[1],x_val.shape[2],1))], y_val),callbacks = callbacks
                validation_data=(x_val, y_val),
                callbacks=callbacks)

            ## fit model
            #            if name_model == "PSPNet":
            #                model.fit(
            #                    x=x_train2,
            #                    y=y_train2,
            #                    batch_size=n_batch,
            #                    epochs=n_epochs,
            #                    class_weight = class_weight,
            #                    validation_data=(x_val2, y_val2),callbacks = callbacks
            #                )
Esempio n. 18
0
x_train = preprocess_input(x_train)

x_val = np.reshape(x_train[-2:, :, :, :], (2, 224, 224, 3))
y_val = np.reshape(y_train[-2:, :, :, :], (2, 224, 224, 4))

x_train = x_train[:-2, :, :, :]
y_train = y_train[:-2, :, :, :]

print(x_train.shape)
print(y_train.shape)

model = Unet(BACKBONE,
             input_shape=(224, 224, 3),
             classes=4,
             encoder_weights='imagenet')
adam = optimizers.Adam(lr=0.0005,
                       beta_1=0.9,
                       beta_2=0.999,
                       epsilon=None,
                       decay=1e-6,
                       amsgrad=True)
model.compile('Adam', loss=bce_jaccard_loss, metrics=[iou_score])

model.fit(x=x_train,
          y=y_train,
          batch_size=8,
          epochs=30,
          validation_data=(x_val, y_val))

model.save('nail_unet.h5', include_optimizer=False)
Esempio n. 19
0
def run(hyperparams):
    print('Running model...')

    #### Begin model input ##########################################################################################

    def get_model(model_json_fname, modelwtsfname):
        # This is only for prediction
        if os.path.isfile(model_json_fname):
            # Model reconstruction from JSON file
            with open(model_json_fname, 'r') as f:
                model = model_from_json(f.read())
        else:
            model = get_unet()

        #model.summary()
        # Load weights into the new model
        model.load_weights(modelwtsfname)
        return model

    def focal_loss(gamma=2., alpha=.25):
        def focal_loss_fixed(y_true, y_pred):
            pt_1 = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred))
            pt_0 = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred))
            return -K.sum(
                alpha * K.pow(1. - pt_1, gamma) * K.log(pt_1)) - K.sum(
                    (1 - alpha) * K.pow(pt_0, gamma) * K.log(1. - pt_0))

        return focal_loss_fixed

    def jaccard_coef(y_true, y_pred):
        smooth = 1.0
        intersection = K.sum(y_true * y_pred, axis=[-0, -1, 2])
        sum_ = K.sum(y_true + y_pred, axis=[-0, -1, 2])

        jac = (intersection + smooth) / (sum_ - intersection + smooth)

        return K.mean(jac)

    def jaccard_coef_int(y_true, y_pred):
        smooth = 1.0
        y_pred_pos = K.round(K.clip(y_pred, 0, 1))

        intersection = K.sum(y_true * y_pred_pos, axis=[-0, -1, 2])
        sum_ = K.sum(y_true + y_pred_pos, axis=[-0, -1, 2])

        jac = (intersection + smooth) / (sum_ - intersection + smooth)

        return K.mean(jac)

    def jaccard_coef_loss(y_true, y_pred):
        return -K.log(jaccard_coef(y_true, y_pred)) + binary_crossentropy(
            y_pred, y_true)

    def dice_coef_batch(y_true, y_pred):
        smooth = 1.0
        intersection = K.sum(y_true * y_pred, axis=[-0, -1, 2])
        sum_ = K.sum(y_true + y_pred, axis=[-0, -1, 2])

        dice = ((2.0 * intersection) + smooth) / (sum_ + intersection + smooth)

        return K.mean(dice)

    def dice_coef(y_true, y_pred):
        smooth = 1.0
        y_true_f = K.flatten(y_true)
        y_pred_f = K.flatten(y_pred)
        intersection = K.sum(y_true_f * y_pred_f)
        dice_smooth = ((2. * intersection) +
                       smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)
        return (dice_smooth)

    def dice_coef_loss(y_true, y_pred):
        return -dice_coef(y_true, y_pred)

    def dice_coef_batch_loss(y_true, y_pred):
        return -dice_coef_batch(y_true, y_pred)

    #Define the neural network
    def get_unet():
        droprate = 0.25
        filt_size = 32
        inputs = Input((None, None, 1))
        conv1 = Conv2D(filt_size, (3, 3), activation='relu',
                       padding='same')(inputs)
        conv1 = Dropout(droprate)(conv1)
        conv1 = Conv2D(filt_size, (3, 3), activation='relu',
                       padding='same')(conv1)
        pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
        filt_size = filt_size * 2

        conv2 = Conv2D(filt_size, (3, 3), activation='relu',
                       padding='same')(pool1)
        conv2 = Dropout(droprate)(conv2)
        conv2 = Conv2D(filt_size, (3, 3), activation='relu',
                       padding='same')(conv2)
        pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
        filt_size = filt_size * 2

        conv3 = Conv2D(filt_size, (3, 3), activation='relu',
                       padding='same')(pool2)
        conv3 = Dropout(droprate)(conv3)
        conv3 = Conv2D(filt_size, (3, 3), activation='relu',
                       padding='same')(conv3)
        pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
        filt_size = filt_size * 2

        conv4 = Conv2D(filt_size, (3, 3), activation='relu',
                       padding='same')(pool3)
        conv4 = Dropout(droprate)(conv4)
        conv4 = Conv2D(filt_size, (3, 3), activation='relu',
                       padding='same')(conv4)
        pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)
        filt_size = filt_size * 2

        conv5 = Conv2D(filt_size, (3, 3), activation='relu',
                       padding='same')(pool4)
        conv5 = Dropout(droprate)(conv5)
        conv5 = Conv2D(filt_size, (3, 3), activation='relu',
                       padding='same')(conv5)

        filt_size = filt_size / 2

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

        filt_size = filt_size / 2

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

        filt_size = filt_size / 2

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

        up9 = concatenate([
            Conv2DTranspose(filt_size, (2, 2), strides=(2, 2),
                            padding='same')(conv8), conv1
        ],
                          axis=3)
        conv9 = Conv2D(filt_size, (3, 3), activation='relu',
                       padding='same')(up9)
        conv9 = Dropout(droprate)(conv9)
        conv9 = Conv2D(filt_size, (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=dice_coef_loss, metrics=[dice_coef])
        #model.compile(optimizer=Nadam(lr=1e-3), loss=dice_coef_loss, metrics=[dice_coef])
        #model.compile(optimizer=Adadelta(), loss=dice_coef_loss, metrics=[dice_coef])

        return model

    def save_model_to_json(model, model_json_fname):

        #model = unet.UResNet152(input_shape=(None, None, 3), classes=1,encoder_weights="imagenet11k")
        #model = get_unet()

        #model.summary()
        # serialize model to JSON
        model_json = model.to_json()
        with open(model_json_fname, "w") as json_file:
            json_file.write(model_json)

    def preprocess_data(do_prediction, inputnpyfname, targetnpyfname,
                        expandChannel, backbone):
        # Preprocess the data (beyond what I already did before)

        print('-' * 30)
        print('Loading and preprocessing data...')
        print('-' * 30)

        # Load, normalize, and cast the data
        imgs_input = (np.load(inputnpyfname).astype('float32') / (2**16 - 1) *
                      (2**8 - 1)).astype('uint8')
        print('Input images information:')
        print(imgs_input.shape)
        print(imgs_input.dtype)
        hist, bins = np.histogram(imgs_input)
        print(hist)
        print(bins)
        if not do_prediction:
            imgs_mask_train = np.load(targetnpyfname).astype('uint8')
            print('Input masks information:')
            print(imgs_mask_train.shape)
            print(imgs_mask_train.dtype)
            hist, bins = np.histogram(imgs_mask_train)
            print(hist)
            print(bins)

        # Make the grayscale images RGB since that's what the model expects apparently
        if expandChannel:
            imgs_input = np.stack((imgs_input, ) * 3, -1)
        else:
            imgs_input = np.expand_dims(imgs_input, 3)
        print('New shape of input images:')
        print(imgs_input.shape)
        if not do_prediction:
            imgs_mask_train = np.expand_dims(imgs_mask_train, 3)
            print('New shape of masks:')
            print(imgs_mask_train.shape)

        # Preprocess as per https://github.com/qubvel/segmentation_models
        preprocessing_fn = get_preprocessing(backbone)
        imgs_input = preprocessing_fn(imgs_input)

        # Return appropriate variables
        if not do_prediction:
            return (imgs_input, imgs_mask_train)
        else:
            return (imgs_input)

    # Import relevant modules and functions
    import sys
    sys.path.append(hyperparams['segmentation_models_repo'])
    import numpy as np
    from keras.models import Model
    from keras.layers import Input, concatenate, Conv2D, MaxPooling2D, Conv2DTranspose, Dropout
    from keras.optimizers import Adam
    from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping, CSVLogger
    from keras.layers.normalization import BatchNormalization
    from keras.backend import binary_crossentropy
    import keras
    import random
    import tensorflow as tf
    from keras.models import model_from_json
    from segmentation_models import Unet
    from segmentation_models.backbones import get_preprocessing
    K.set_image_data_format(
        'channels_last')  # TF dimension ordering in this code

    # Basically constants
    expandChannel = True
    modelwtsfname = 'model_weights.h5'
    model_json_fname = 'model.json'
    csvfname = 'model.csv'

    do_prediction = hyperparams['predict']
    if not do_prediction:  # Train...
        print('Training...')

        # Parameters
        inputnpyfname = hyperparams['images']
        labels = hyperparams['labels']
        initialize = hyperparams['initialize']
        backbone = hyperparams['backbone']
        encoder = hyperparams['encoder']
        lr = float(hyperparams['lr'])
        batch_size = hyperparams['batch_size']
        obj_return = hyperparams['obj_return']
        epochs = hyperparams['epochs']

        # Preprocess the data
        imgs_train, imgs_mask_train = preprocess_data(do_prediction,
                                                      inputnpyfname, labels,
                                                      expandChannel, backbone)
        # Load, save, and compile the model
        model = Unet(backbone_name=backbone, encoder_weights=encoder)
        save_model_to_json(model, model_json_fname)
        model.compile(optimizer=Adam(lr=lr),
                      loss='binary_crossentropy',
                      metrics=[
                          'binary_crossentropy', 'mean_squared_error',
                          dice_coef, dice_coef_batch,
                          focal_loss()
                      ])
        # Load previous weights for restarting, if desired and possible
        if os.path.isfile(initialize):
            print('-' * 30)
            print('Loading previous weights ...')
            model.load_weights(initialize)
        # Set up the training callback functions
        model_checkpoint = ModelCheckpoint(modelwtsfname,
                                           monitor=obj_return,
                                           save_best_only=True)
        reduce_lr = ReduceLROnPlateau(monitor=obj_return,
                                      factor=0.1,
                                      patience=100,
                                      min_lr=0.001,
                                      verbose=1)
        model_es = EarlyStopping(monitor=obj_return,
                                 min_delta=0.00000001,
                                 patience=100,
                                 verbose=1,
                                 mode='auto')
        csv_logger = CSVLogger(csvfname, append=True)
        # Train the model
        history_callback = model.fit(
            imgs_train,
            imgs_mask_train,
            batch_size=batch_size,
            epochs=epochs,
            verbose=2,
            shuffle=True,
            validation_split=0.10,
            callbacks=[model_checkpoint, reduce_lr, model_es, csv_logger])
        print("Minimum validation loss:")
        print(min(history_callback.history[obj_return]))
    else:  # ...or predict
        print('Inferring...')

        # Parameters
        inputnpyfname = hyperparams['images']
        initialize = hyperparams['initialize']
        backbone = hyperparams['backbone']
        # lr = float(hyperparams['lr']) # this isn't needed but we're keeping it for the U-Net, where it is "needed"

        # Preprocess the data
        imgs_infer = preprocess_data(do_prediction, inputnpyfname, '',
                                     expandChannel, backbone)
        # Load the model
        #model = get_model(model_json_fname,initialize)
        model = get_model(
            os.path.dirname(initialize) + '/' + model_json_fname, initialize)

        # Run inference
        imgs_test_predict = model.predict(imgs_infer, batch_size=1, verbose=1)
        # Save the predicted masks
        np.save('mask_predictions.npy',
                np.squeeze(np.round(imgs_test_predict).astype('uint8')))
        history_callback = None

    #### End model input ############################################################################################

    return (history_callback)
Esempio n. 20
0
    iou = K.mean(iou * class_weights)

    return iou


test_images = []
labels = []
for i in range(100):
    test_images.append(np.random.rand(128, 128, 3))
    rand = np.random.randint(10, size=(128, 128))
    labels.append(rand)
test_images = np.array(test_images)
labels = np.array(labels)
x_train, x_val, y_train, y_val = train_test_split(test_images,
                                                  labels,
                                                  test_size=0.33,
                                                  random_state=42)
y_train = to_categorical(y_train)
y_val = to_categorical(y_val)
print(x_train.shape)
print(y_train.shape)
model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=[iou_score])
model.fit(
    x=x_train,
    y=y_train,
    batch_size=16,
    epochs=100,
    validation_data=(x_val, y_val),
)
Esempio n. 21
0
              loss='categorical_crossentropy',
              metrics=['categorical_accuracy'])

####################################################
############# Training model #######################
####################################################

for i in range(n_save):
    print('==============================')
    print('in iteration: ', i + 1)
    print('==============================')

    model.fit(x,
              y_train,
              validation_data=(X_val, y_val),
              callbacks=[TestCallback((X_val, y_val))],
              batch_size=1,
              epochs=epc_num,
              verbose=True)
    mname = 'PSPNet_relabeled_' + str(epc_num * (i + 1)) + '.h5'

    print('==============================')
    print('saving model after epoch ', (i + 1) * epc_num)
    print('==============================')

    #     save_name = model_name+"_noaug_epoch_"+str(10*(i+1))+".h5"
    model.save(mname)

# ## Result visualization

# In[5]: