Ejemplo n.º 1
0
def train():
    print('Loading and preprocessing train data...')
    print('-' * 30)

    imgs_train, imgs_mask_train = load_train_data()
    print('Loaded train images: ', imgs_train.shape, imgs_mask_train.shape)
    print('-' * 30)

    # Normalization of the train set (Exp 4)
    imgs_train = rescale_intensity(imgs_train, in_range=(0, 1))

    imgs_train = imgs_train.astype(np.float32)
    imgs_mask_train = imgs_mask_train.astype(np.float32)

    print('Creating and compiling model...')
    print('-' * 30)

    model = unet()
    #Saving the weights and the loss of the best predictions we obtained
    model_checkpoint = ModelCheckpoint(
        '/data/flavio/anatiel/models/dissertacao/unet_exp4_200epc_best_int16_clahe.h5',
        monitor='val_loss',
        save_best_only=True,
        mode="min")

    print('Fitting model...')
    print('-' * 30)
    history = model.fit(imgs_train,
                        imgs_mask_train,
                        batch_size=BATCH_SIZE,
                        epochs=EPOCHS,
                        verbose=1,
                        shuffle=True,
                        validation_split=0.1,
                        callbacks=[model_checkpoint])

    model.save(
        '/data/flavio/anatiel/models/dissertacao/unet_exp4_200epc_last_int16_clahe.h5'
    )

    # convert the history.history dict to a pandas DataFrame:
    hist_df = pd.DataFrame(history.history)

    # save to json:
    hist_json_file = '/data/flavio/anatiel/models/dissertacao/unet_exp4_history_200epc_int16_clahe.json'
    with open(hist_json_file, mode='w') as f:
        hist_df.to_json(f)
    print("history saved")

    plt.plot(history.history['dice_coef'])
    plt.plot(history.history['val_dice_coef'])
    plt.title('Model dice coeff')
    plt.ylabel('Dice coeff')
    plt.xlabel('Epoch')
    plt.legend(['Train', 'Val'], loc='upper left')
    # save plot to file
    plt.savefig(
        '/data/flavio/anatiel/models/dissertacao/unet_exp4_plot_200epc_int16_clahe.png'
    )
Ejemplo n.º 2
0
def train():
    print('Loading and preprocessing train data...')
    print('-' * 30)

    imgs_train, imgs_mask_train = load_train_data()
    print('Loaded train images: ', imgs_train.shape, imgs_train.dtype,
          imgs_mask_train.shape, imgs_mask_train.dtype)
    print('-' * 30)

    # mean = np.mean(imgs_train)  # mean for data centering
    # std = np.std(imgs_train)  # std for data normalization

    # Normalization of the train set (Exp 1)
    # imgs_train = imgs_train.astype(np.float32)
    # imgs_train -= mean
    # imgs_train /= std

    # imgs_train = imgs_train.astype(np.float32)
    # imgs_mask_train = imgs_mask_train.astype(np.float32)

    print('Creating and compiling model...')
    print('-' * 30)

    model = unet()
    #Saving the weights and the loss of the best predictions we obtained
    model_checkpoint = ModelCheckpoint(
        f'/data/flavio/anatiel/models/models_kfold/unet_ds{DS}_150epc_best_k{K}.h5',
        monitor='val_loss',
        save_best_only=True,
        mode="min")

    print('Fitting model...')
    print('-' * 30)
    history = model.fit(imgs_train,
                        imgs_mask_train,
                        batch_size=BATCH_SIZE,
                        epochs=EPOCHS,
                        verbose=1,
                        shuffle=True,
                        validation_split=0.1,
                        callbacks=[model_checkpoint])

    model.save(
        f'/data/flavio/anatiel/models/models_kfold/unet_ds{DS}_150epc_last_k{K}.h5'
    )

    # convert the history.history dict to a pandas DataFrame:
    hist_df = pd.DataFrame(history.history)

    # save to json:
    hist_json_file = f'/data/flavio/anatiel/models/models_kfold/unet_ds{DS}_150epc_k{K}.json'
    with open(hist_json_file, mode='w') as f:
        hist_df.to_json(f)
    print("history saved")

    plt.plot(history.history['dice_coef'])
    plt.plot(history.history['val_dice_coef'])
    plt.title('Model dice coeff')
    plt.ylabel('Dice coeff')
    plt.xlabel('Epoch')
    plt.legend(['Train', 'Val'], loc='upper left')
    # save plot to file
    plt.savefig(
        f'/data/flavio/anatiel/models/models_kfold/unet_ds{DS}_150epc_k{K}.png'
    )
Ejemplo n.º 3
0
def train():
    print('Loading and preprocessing train data...')
    print('-' * 30)

    imgs_train, imgs_mask_train = load_train_data()
    print('Loaded train images: ', imgs_train.shape, imgs_mask_train.shape)
    print('-' * 30)

    imgs_val, imgs_mask_val = load_val_data()
    print('Loaded val images: ', imgs_val.shape, imgs_mask_val.shape)
    print('-' * 30)

    imgs_train = imgs_train.astype('float32')
    mean = np.mean(imgs_train)  # mean for data centering
    std = np.std(imgs_train)  # std for data normalization

    #Normalization of the train set
    imgs_train -= mean
    imgs_train /= std
    imgs_mask_train = imgs_mask_train.astype('float32')

    print('Creating and compiling model...')
    print('-' * 30)

    model = unet()
    #Saving the weights and the loss of the best predictions we obtained
    model_checkpoint = ModelCheckpoint(
        '/data/flavio/anatiel/models/unet2d/weights_unet_masked_lung_clahe_500epc.h5',
        monitor='val_loss',
        save_best_only=True)
    # checkpoint = ModelCheckpoint('/data/flavio/anatiel/models/unet2d/weights_train_unet_masked_lung_blur_3epc.h5', monitor='dice', verbose=1, save_best_only=True,save_weights_only=True, mode='max')
    # checkpoint2 = ModelCheckpoint('/data/flavio/anatiel/models/unet2d/weights_val_unet_masked_lung_blur_3epc.h5', monitor='val_dice', verbose=1, save_best_only=True,save_weights_only=True, mode='max')

    print('Fitting model...')
    print('-' * 30)
    history = model.fit(imgs_train,
                        imgs_mask_train,
                        batch_size=BATCH_SIZE,
                        epochs=EPOCHS,
                        verbose=1,
                        shuffle=True,
                        validation_split=0.2,
                        callbacks=[model_checkpoint])
    # history = model.fit(imgs_train, imgs_mask_train, batch_size=BATCH_SIZE, epochs=EPOCHS, verbose=1, shuffle=True, callbacks=[checkpoint,checkpoint2], validation_data=(imgs_val, imgs_mask_val))

    model.save(
        '/data/flavio/anatiel/models/unet2d/weights_unet_masked_lung_clahe_500epc_last.h5'
    )

    # convert the history.history dict to a pandas DataFrame:
    hist_df = pd.DataFrame(history.history)

    # save to json:
    print("saving history...")
    hist_json_file = 'anatiel/unet/history_masked_lung_clahe_500epc.json'
    with open(hist_json_file, mode='w') as f:
        hist_df.to_json(f)
    print("history saved")

    plt.plot(history.history['dice_coef'])
    plt.plot(history.history['val_dice_coef'])
    plt.title('Model dice coeff')
    plt.ylabel('Dice coeff')
    plt.xlabel('Epoch')
    plt.legend(['Train', 'Test'], loc='upper left')
    # save plot to file
    plt.savefig('anatiel/unet/plot_dice_masked_lung_clahe_500epc.png')
    plt.show()