def main(): config = configparser.RawConfigParser() config.read('config.txt') experiment_name = config.get('train', 'name') if not os.path.exists('./logs/' + experiment_name): os.system('mkdir ./logs/' + experiment_name) epochs_num = int(config.get('train', 'epochs_num')) batch_size = int(config.get('train', 'batch_size')) # Load datasets. datasets = config.get('train', 'datasets') datasets_path = config.get(datasets, 'h5py_save_path') height = int(config.get(datasets, 'height')) width = int(config.get(datasets, 'width')) pad_height = int(config.get(datasets, 'pad_height')) pad_width = int(config.get(datasets, 'pad_width')) x_train, y_train, masks = Generator(datasets_path, 'train', height, width, pad_height, pad_width)() visualize(group_images(x_train, 4), './logs/' + experiment_name + '/train_images.png').show() visualize(group_images(y_train, 4), './logs/' + experiment_name + '/train_labels.png').show() visualize(group_images(masks, 4), './logs/' + experiment_name + '/train_masks.png').show() y_train = to_categorical(y_train) # Build model and save. unet = Unet((pad_height, pad_width, 1), 5) unet.summary() unet_json = unet.to_json() open('./logs/' + experiment_name + '/architecture.json', 'w').write(unet_json) plot_model(unet, to_file='./logs/' + experiment_name + '/model.png') # Training. checkpointer = ModelCheckpoint(filepath='./logs/' + experiment_name + '/weights.h5', verbose=1, monitor='val_loss', mode='auto', save_best_only=True) unet.fit( x_train, y_train, epochs=epochs_num, batch_size=batch_size, verbose=1, shuffle=True, validation_split=0.1, #class_weight=(0.5, 1.3), callbacks=[checkpointer])
print(np.max(x_train), np.min(x_train), x_train.shape, x_train.dtype) print(np.max(y_train), np.min(y_train), y_train.shape, y_train.dtype) # Build model and save. unet = Unet((sub_height, sub_width, 1)) unet.summary() unet_json = unet.to_json() open('./logs/' + experiment_name + '_architecture.json', 'w').write(unet_json) plot_model(unet, to_file='./logs/' + experiment_name + '_model.png') # Training. checkpointer = ModelCheckpoint(filepath='./logs/' + experiment_name + '_best_weights.h5', verbose=1, monitor='val_loss', mode='auto', save_best_only=True) unet.fit(x_train, y_train, epochs=epochs_num, batch_size=batch_size, verbose=1, shuffle=True, validation_split=0.1, callbacks=[checkpointer]) unet.save_weights('./logs/' + experiment_name + '_last_weights.h5', overwrite=True)
sys.path.insert(1, './src') from unet import Unet from kerasaug import DataGenerator if not os.path.exists(str("./" + dest)): os.mkdir(str("./" + dest)) os.chdir(str("./" + dest)) strategy = tf.distribute.experimental.MultiWorkerMirroredStrategy() tf_config_json = os.environ.get("TF_CONFIG", "{}") tf_config = json.loads(tf_config_json) with strategy.scope(): model = Unet(num_class=1) trainset = DataGenerator("../dataset/SNEMI3D_mito/train", batch_size=batch_size) validset = DataGenerator("../dataset/SNEMI3D_mito/valid", batch_size=batch_size) history = model.fit(trainset, steps_per_epoch=steps_per_epoch, epochs=epochs, validation_data=validset, validation_steps=validation_steps, verbose=1) model.save('./seg_mito_keras.h5') plt.plot(history.history['accuracy'], label='training accuracy') plt.plot(history.history['val_accuracy'], label='validation accuracy') plt.title('training and validation accuracy') plt.xlabel("epoch") plt.ylabel("accuracy") plt.legend() plt.savefig('./seg_mito_keras_acc.png') plt.plot(history.history['loss'], label='training loss') plt.plot(history.history['val_loss'], label='validation loss') plt.title('Training and Validation loss')