def train(opt): #####Compile mode#### input_size = (opt.row, opt.col, opt.ch) SegM = SegModel(input_size) model = SegM.model _adam = optimizers.Adam(lr=opt.lr, beta_1=0.9, beta_2=0.999, decay=0.0) model.compile(loss='binary_crossentropy', optimizer=_adam, metrics=['accuracy']) ###################load data########### img_dim = (opt.row, opt.col) [train_img, train_mask, test_img, test_mask] = dataloader(opt.ad1, opt.ad2, opt.ad3, opt.ad5, opt.img_format, img_dim) ####check point#### model_checkpoint = ModelCheckpoint(opt.chekp + '.hdf5', monitor='val_acc', verbose=1, save_best_only=True) ###################train########### hist = model.fit(train_img, train_mask, validation_data=(test_img, test_mask), batch_size=opt.batch_size, nb_epoch=opt.epochs, verbose=1, callbacks=[model_checkpoint]) # ######evaluate##### model.load_weights(opt.chekp + '.hdf5') y_pred = model.predict(test_img) f = fscore(y_pred, test_img, test_mask) print(f) return hist, f
return prediction def loss_function(real, preds): # y_error = tf.subtract(real,preds) # return tf.nn.l2_loss(y_error) return tf.losses.absolute_difference(real, preds) # return tf.losses.mean_squared_error(real,preds) print("version of tensorflow:", tf.__version__) tf.enable_eager_execution() tms = utils.buildAllTMToATensor(xmlPath="../traffic-matrices/") data_loader = dataloader(tms, EMA=True, alpha=0.9) model = Model(out_size=23 * 23, units=23 * 23) # Using adam optimizer with default arguments lr = 0.01 optimizer = tf.train.AdamOptimizer(learning_rate=lr) BATCH_SIZE = 32 seq_length = 20 ds_list = [0, 1, 1, 1, 1, 1, 1, 1, 1, 1] model.build(tf.TensorShape([BATCH_SIZE, len(ds_list), 23 * 23])) model.summary() # Directory where the checkpoints will be saved checkpoint_path = './training_checkpoints.ckpt' loss_record = []
print("version of tensorflow:", tf.__version__) # tf.enable_eager_execution() # ds_1_end =list(range(1,10)) # ds_list=[0] ds_list = list(range(0, 100, 2)) target_pos = 1 batchsize = 64 #**************prepare data********************* tms = utils.buildAllTMToATensor(xmlPath="../traffic-matrices/") # data_loader = dataloader(tms,EMA=False,alpha=0.9) data_loader = dataloader(tms, ratio_for_train=0.85, EMA=False, alpha=0.1, BOX=True, normalization=1) train_x, train_y = [], [] # gen = data_loader.iter_for_train(ds_list = ds_list,batchsize=1,target_pos=1 ) for x, y in data_loader.iter_for_train(ds_list=ds_list, batchsize=1, target_pos=target_pos): b, l, m, n = x.shape b_, m_, n_ = y.shape # tmp_x = train_x.append(x.reshape([l * m * n])) train_y.append(y.reshape([m_ * n_])) train_x = np.array(train_x)