# Creating the op for initializing all variables init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) global_step = 0 # Number of training iterations in each epoch num_tr_iter = int(len(y_train) / batch_size) for epoch in range(epochs): print('Training epoch: {}'.format(epoch + 1)) x_train, y_train = randomize(x_train, y_train) for iteration in range(num_tr_iter): global_step += 1 start = iteration * batch_size end = (iteration + 1) * batch_size x_batch, y_batch = get_next_batch(x_train, y_train, start, end) x_batch = x_batch.reshape((batch_size, max_time, input_dim)) # Run optimization op (backprop) feed_dict_batch = {x: x_batch, y: y_batch} sess.run(optimizer, feed_dict=feed_dict_batch) if iteration % display_freq == 0: # Calculate and display the batch loss and accuracy loss_batch, acc_batch = sess.run([loss, accuracy], feed_dict=feed_dict_batch) print("iter {0:3d}:\t Loss={1:.2f},\tTraining Accuracy={2:.01%}". format(iteration, loss_batch, acc_batch)) # Run validation after every epoch
import utils import tensorflow as tf CFG_PATH = 'cfg/yolov3_tiny_traffic_inference.cfg' DARKNET_WEIGHTS_PATH = '/home/diendl/tiny_model/yolov3_tiny_traffic_train_4000.weights' LABELMAP = 'cfg/coco.names' TRAIN_FILES = ['dataset_tools/train.tf'] model = YOLOv3(CFG_PATH) graph = tf.Graph() with graph.as_default(): dataset = utils.create_dataset(TRAIN_FILES, model.BATCH, model.HEIGHT, model.WIDTH, model.CHANNELS) images_batch, dense_indices_batch, dense_bxs_batch, dense_bys_batch, dense_bws_batch, dense_bhs_batch \ = utils.get_next_batch(dataset) inputs = tf.placeholder( dtype=tf.float32, name='input_images', shape=[model.BATCH, model.WIDTH, model.HEIGHT, model.CHANNELS]) weights_list, predictions = model.forward(inputs) load_weights_ops = utils.load_darknet_weights(DARKNET_WEIGHTS_PATH, weights_list) num_detections = [(item**2) * 3 for item in model.GRID_SIZES] num_detections = sum(num_detections) outputs = tf.placeholder( shape=[model.BATCH, num_detections, model.NUM_CLASSES + 5], dtype=tf.float32,
def main(args): #先看数据集数据是否存在 if not os.path.exists(args.dataset_train_dir) or not os.path.exists( args.dataset_validate_dir): raise NameError( '数据集路径"./dataset/MIR-1K/Wavfile"或"./dataset/MIR-1K/UndividedWavfile"不存在!' ) # 1. 导入需要训练的数据集文件路径,存到列表中即可 train_file_list = load_file(args.dataset_train_dir) valid_file_list = load_file(args.dataset_validate_dir) # 数据集的采样率 mir1k_sr = args.dataset_sr # 用于短时傅里叶变换,窗口大小 n_fft = 1024 # 步幅;帧移对应卷积中的stride; hop_length = n_fft // 4 # Model parameters # 学习率 learning_rate = args.learning_rate # 用于创建rnn节点数 num_hidden_units = [1024, 1024, 1024, 1024, 1024] # batch 长度 batch_size = args.batch_size # 获取多少帧数据 sample_frames = args.sample_frames # 训练迭代次数 iterations = args.iterations # dropout dropout_rate = args.dropout_rate # 模型保存路径 model_dir = args.model_dir model_filename = args.model_filename #导入训练数据集的wav数据, #wavs_mono_train存的是单声道,wavs_music_train 存的是背景音乐,wavs_voice_train 存的是纯人声 wavs_mono_train, wavs_music_train, wavs_voice_train = load_wavs( filenames=train_file_list, sr=mir1k_sr) # 通过短时傅里叶变换将声音转到频域 stfts_mono_train, stfts_music_train, stfts_voice_train = wavs_to_specs( wavs_mono=wavs_mono_train, wavs_music=wavs_music_train, wavs_voice=wavs_voice_train, n_fft=n_fft, hop_length=hop_length) # 跟上面一样,只不过这里是测试集的数据 wavs_mono_valid, wavs_music_valid, wavs_voice_valid = load_wavs( filenames=valid_file_list, sr=mir1k_sr) stfts_mono_valid, stfts_music_valid, stfts_voice_valid = wavs_to_specs( wavs_mono=wavs_mono_valid, wavs_music=wavs_music_valid, wavs_voice=wavs_voice_valid, n_fft=n_fft, hop_length=hop_length) #初始化模型 model = SVMRNN(num_features=n_fft // 2 + 1, num_hidden_units=num_hidden_units) # 加载模型,如果没有模型,则初始化所有变量 startepo = model.load(file_dir=model_dir) print('startepo:' + str(startepo)) #开始训练 for i in (range(iterations)): #从模型中断处开始训练 if i < startepo: continue # 获取下一batch数据 data_mono_batch, data_music_batch, data_voice_batch = get_next_batch( stfts_mono=stfts_mono_train, stfts_music=stfts_music_train, stfts_voice=stfts_voice_train, batch_size=batch_size, sample_frames=sample_frames) #获取频率值 x_mixed_src, _ = separate_magnitude_phase(data=data_mono_batch) y_music_src, _ = separate_magnitude_phase(data=data_music_batch) y_voice_src, _ = separate_magnitude_phase(data=data_voice_batch) #送入神经网络,开始训练 train_loss = model.train(x_mixed_src=x_mixed_src, y_music_src=y_music_src, y_voice_src=y_voice_src, learning_rate=learning_rate, dropout_rate=dropout_rate) if i % 10 == 0: print('Step: %d Train Loss: %f' % (i, train_loss)) if i % 200 == 0: #这里是测试模型准确率的 print('==============================================') data_mono_batch, data_music_batch, data_voice_batch = get_next_batch( stfts_mono=stfts_mono_valid, stfts_music=stfts_music_valid, stfts_voice=stfts_voice_valid, batch_size=batch_size, sample_frames=sample_frames) x_mixed_src, _ = separate_magnitude_phase(data=data_mono_batch) y_music_src, _ = separate_magnitude_phase(data=data_music_batch) y_voice_src, _ = separate_magnitude_phase(data=data_voice_batch) y_music_src_pred, y_voice_src_pred, validate_loss = model.validate( x_mixed_src=x_mixed_src, y_music_src=y_music_src, y_voice_src=y_voice_src, dropout_rate=dropout_rate) print('Step: %d Validation Loss: %f' % (i, validate_loss)) print('==============================================') if i % 200 == 0: model.save(directory=model_dir, filename=model_filename, global_step=i)
def train(opt, netG, loader_objs, device): # Optimizer optimizer_netG = optim.Adamax(netG.parameters(), lr=opt.lr) # Discriminator netD_img, netD_seq, optimizer_netD = create_netD(opt, device) train_dataloader = loader_objs['train_dataloader'] test_dataloader = loader_objs['test_dataloader'] n_train_batches = loader_objs['n_train_batches'] n_test_batches = loader_objs['n_test_batches'] total_step = 0 start_time = time.time() for epoch in range(opt.epoch): utils.update_learning_rate(optimizer_netG, decay_rate=0.99, lowest=opt.lr / 10) utils.update_learning_rate(optimizer_netD, decay_rate=0.99, lowest=opt.lr / 10) for it in range(n_train_batches): data_dict = utils.get_data_dict(train_dataloader) batch_dict = utils.get_next_batch(data_dict) res = netG.compute_all_losses(batch_dict) loss_netG = res["loss"] # Compute Adversarial Loss real = batch_dict["data_to_predict"] fake = res["pred_y"] input_real = batch_dict["observed_data"] # Filter out mask if opt.irregular: b, _, c, h, w = real.size() observed_mask = batch_dict["observed_mask"] mask_predicted_data = batch_dict["mask_predicted_data"] selected_timesteps = int(observed_mask[0].sum()) input_real = input_real[observed_mask.squeeze(-1).byte(), ...].view(b, selected_timesteps, c, h, w) real = real[mask_predicted_data.squeeze(-1).byte(), ...].view(b, selected_timesteps, c, h, w) loss_netD = opt.lamb_adv * netD_seq.netD_adv_loss(real, fake, input_real) loss_netD += opt.lamb_adv * netD_img.netD_adv_loss(real, fake, None) loss_adv_netG = opt.lamb_adv * netD_seq.netG_adv_loss(fake, input_real) loss_adv_netG += opt.lamb_adv * netD_img.netG_adv_loss(fake, None) loss_netG += loss_adv_netG # Train D optimizer_netD.zero_grad() loss_netD.backward() optimizer_netD.step() # Train G optimizer_netG.zero_grad() loss_netG.backward() optimizer_netG.step() if (total_step + 1) % opt.log_print_freq == 0 or total_step == 0: et = time.time() - start_time et = str(datetime.timedelta(seconds=et))[:-7] log = f"Elapsed [{et}] Epoch [{epoch:03d}/{opt.epoch:03d}]\t"\ f"Iterations [{(total_step + 1):6d}] \t"\ f"Mse [{res['loss'].item():.4f}]\t"\ f"Adv_G [{loss_adv_netG.item():.4f}]\t"\ f"Adv_D [{loss_netD.item():.4f}]" print(log) if (total_step + 1) % opt.ckpt_save_freq == 0 or (epoch + 1 == opt.epoch and it + 1 == n_train_batches) or total_step == 0: utils.save_checkpoint(netG, os.path.join(opt.checkpoint_dir, f"ckpt_{(total_step + 1):08d}.pth")) if (total_step + 1) % opt.image_print_freq == 0 or total_step == 0: gt, pred, time_steps = visualize.make_save_sequence(opt, batch_dict, res) if opt.extrap: visualize.save_extrap_images(opt=opt, gt=gt, pred=pred, path=opt.train_image_path, total_step=total_step) else: visualize.save_interp_images(opt=opt, gt=gt, pred=pred, path=opt.train_image_path, total_step=total_step) total_step += 1 # Test if (epoch + 1) % 100 == 0: test(netG, epoch, test_dataloader, opt, n_test_batches)
def train(data, sample_size_X, sample_size_Y, conv_layer_specs, dense_layer_specs, alpha_XY, m_XY, optimizer=lasagne.updates.rmsprop, batch_size=20, epoch_size=100, initial_patience=1000, improvement_threshold=0.99, patience_increase=5, max_iter=100000): ''' Utility function for training a siamese net for cross-modality hashing Assumes data['X_train'][n] should be mapped close to data['Y_train'][m] only when n == m :parameters: - data : dict of list of np.ndarray Training/validation sequences in X/Y modality Should contain keys X_train, Y_train, X_validate, Y_validate Sequence matrix shape=(n_sequences, n_time_steps, n_features) - sample_size_X, sample_size_Y : int Sampled sequence length for X/Y modalities - conv_layer_specs, dense_layer_specs : list of dict List of dicts, where each dict corresponds to keyword arguments for each subsequent layer. Note that dense_layer_specs[-1]['num_units'] should be the output dimensionality of the network. - alpha_XY : float Scaling parameter for cross-modality negative example cost - m_XY : int Cross-modality negative example threshold - optimizer: function Function which takes a Theano expression and parameters and computes parameter updates to minimize the Theano expression (for example, something from lasagne.updates). - batch_size : int Mini-batch size - epoch_size : int Number of mini-batches per epoch - initial_patience : int Always train on at least this many batches - improvement_threshold : float Validation cost must decrease by this factor to increase patience - patience_increase : int How many more epochs should we wait when we increase patience - max_iter : int Maximum number of batches to train on :returns: - epoch : iterator Results for each epoch are yielded ''' # Create networks layers = { 'X': utils.build_network( (None, None, data['X_train'][0].shape[-1]), conv_layer_specs, dense_layer_specs), 'Y': utils.build_network( (None, None, data['Y_train'][0].shape[-1]), conv_layer_specs, dense_layer_specs)} # Inputs to X modality neural nets X_p_input = T.tensor3('X_p_input') X_n_input = T.tensor3('X_n_input') # Y network Y_p_input = T.tensor3('Y_p_input') Y_n_input = T.tensor3('Y_n_input') # Compute \sum max(0, m - ||a - b||_2)^2 def hinge_cost(m, a, b): dist = m - T.sqrt(T.sum((a - b)**2, axis=1)) return T.mean((dist*(dist > 0))**2) def hasher_cost(deterministic): X_p_output = lasagne.layers.get_output( layers['X']['out'], {layers['X']['in']: X_p_input}, deterministic=deterministic) X_n_output = lasagne.layers.get_output( layers['X']['out'], {layers['X']['in']: X_n_input}, deterministic=deterministic) Y_p_output = lasagne.layers.get_output( layers['Y']['out'], {layers['Y']['in']: Y_p_input}, deterministic=deterministic) Y_n_output = lasagne.layers.get_output( layers['Y']['out'], {layers['Y']['in']: Y_n_input}, deterministic=deterministic) # Unthresholded, unscaled cost of positive examples across modalities cost_p = T.mean(T.sum((X_p_output - Y_p_output)**2, axis=1)) # Thresholded, scaled cost of cross-modality negative examples cost_n = alpha_XY*hinge_cost(m_XY, X_n_output, Y_n_output) # Sum positive and negative costs for overall cost cost = cost_p + cost_n return cost # Combine all parameters from both networks params = (lasagne.layers.get_all_params(layers['X']['out']) + lasagne.layers.get_all_params(layers['Y']['out'])) # Compute RMSProp gradient descent updates updates = optimizer(hasher_cost(False), params) # Function for training the network train = theano.function([X_p_input, X_n_input, Y_p_input, Y_n_input], hasher_cost(False), updates=updates) # Compute cost without training cost = theano.function([X_p_input, X_n_input, Y_p_input, Y_n_input], hasher_cost(True)) # Compute output without training X_output = theano.function( [layers['X']['in'].input_var], lasagne.layers.get_output(layers['X']['out'], deterministic=True)) Y_output = theano.function( [layers['Y']['in'].input_var], lasagne.layers.get_output(layers['Y']['out'], deterministic=True)) # Start with infinite validate cost; we will always increase patience once current_validate_cost = np.inf patience = initial_patience # Create sampled sequences for validation X_validate = utils.sample_sequences( data['X_validate'], sample_size_X) Y_validate = utils.sample_sequences( data['Y_validate'], sample_size_Y) # Create fixed negative example validation set X_validate_shuffle = np.random.permutation(X_validate.shape[0]) Y_validate_shuffle = X_validate_shuffle[ utils.random_derangement(X_validate.shape[0])] # Create iterator to sample sequences from training data data_iterator = utils.get_next_batch( data['X_train'], data['Y_train'], sample_size_X, sample_size_Y, batch_size, max_iter) # We will accumulate the mean train cost over each epoch train_cost = 0 for n, (X_p, Y_p, X_n, Y_n) in enumerate(data_iterator): # Occasionally Theano was raising a MemoryError, this fails gracefully try: train_cost += train(X_p, X_n, Y_p, Y_n) except MemoryError as e: print "MemoryError: {}".format(e) return # Stop training if a NaN is encountered if not np.isfinite(train_cost): print 'Bad training cost {} at iteration {}'.format(train_cost, n) break # Validate the net after each epoch if n and (not n % epoch_size): epoch_result = collections.OrderedDict() epoch_result['iteration'] = n # Compute average training cost over the epoch epoch_result['train_cost'] = train_cost / float(epoch_size) # Reset training cost mean accumulation train_cost = 0 # We need to accumulate the validation cost and network output over # batches to avoid MemoryErrors epoch_result['validate_cost'] = 0 validate_batches = 0 X_val_output = [] Y_val_output = [] for batch_idx in range(0, X_validate.shape[0], batch_size): # Extract slice from validation set for this batch batch_slice = slice(batch_idx, batch_idx + batch_size) # Compute and accumulate cost epoch_result['validate_cost'] += cost( X_validate[batch_slice], X_validate[X_validate_shuffle][batch_slice], Y_validate[batch_slice], Y_validate[Y_validate_shuffle][batch_slice]) # Keep track of # of batches for normalization validate_batches += 1 # Compute network output and accumulate result X_val_output.append(X_output(X_validate[batch_slice])) Y_val_output.append(Y_output(Y_validate[batch_slice])) # Normalize cost by number of batches and store epoch_result['validate_cost'] /= float(validate_batches) # Concatenate per-batch output to tensors X_val_output = np.concatenate(X_val_output, axis=0) Y_val_output = np.concatenate(Y_val_output, axis=0) # Compute in-class and out-of-class distances in_dists = np.mean((X_val_output - Y_val_output)**2, axis=1) out_dists = np.mean((X_val_output[X_validate_shuffle] - Y_val_output[Y_validate_shuffle])**2, axis=1) # Objective is Bhattacharrya coefficient of in-class and # out-of-class distances epoch_result['validate_objective'] = utils.bhatt_coeff( in_dists, out_dists) # Test whether this validate cost is the new smallest if epoch_result['validate_cost'] < current_validate_cost: # To update patience, we must be smaller than # improvement_threshold*(previous lowest validation cost) patience_cost = improvement_threshold*current_validate_cost if epoch_result['validate_cost'] < patience_cost: # Increase patience by the supplied about patience += epoch_size*patience_increase # Even if we didn't increase patience, update lowest valid cost current_validate_cost = epoch_result['validate_cost'] # Store patience after this epoch epoch_result['patience'] = patience # Yield scores and statistics for this epoch X_params = lasagne.layers.get_all_param_values(layers['X']['out']) Y_params = lasagne.layers.get_all_param_values(layers['Y']['out']) yield (epoch_result, X_params, Y_params) if n > patience: break return
model.add(Dense(100, activation='relu')) # model.add(Dense(50, activation='relu')) # model.add(Dense(10, activation='relu')) # model.add(Dense(1)) model.compile(optimizer=Adam(learning_rate), loss="mse") return model if __name__ == "__main__": model = get_model() model.summary() # create two generators for training and validation train_gen = utils.get_next_batch() validation_gen = utils.get_next_batch() # model.fit_generator(train_gen, steps_per_epoch=n_samples_per_epoch // batch_size, epochs=n_epochs, validation_data=validation_gen, validation_steps=n_valid_samples // batch_size, verbose=1) # save model model.save('model.h5')
def validation(x_valid, y_valid, val_batch_size, num_classes, sess, model, epoch, start_time, w_plus): loss_batch_all = np.array([]) acc_batch_all = y_pred_all = logits_all = np.zeros((0, num_classes)) model.is_train = False x_valid, y_valid = randomize(x_valid, y_valid) step_count = int(len(x_valid) / val_batch_size) for step in range(step_count): start = step * val_batch_size end = (step + 1) * val_batch_size x_batch, y_batch = get_next_batch(x_valid, y_valid, start, end) feed_dict_val = {model.x: x_batch, model.y: y_batch, model.w_plus: w_plus} acc_valid, loss_valid, y_pred, logits = sess.run( [model.accuracy, model.loss, model.prediction, model.get_logits], feed_dict=feed_dict_val) acc_batch_all = np.concatenate((acc_batch_all, acc_valid.reshape([1, num_classes]))) y_pred_all = np.concatenate((y_pred_all, y_pred.reshape([val_batch_size, num_classes]))) logits_all = np.concatenate((logits_all, logits.reshape([val_batch_size, num_classes]))) loss_batch_all = np.append(loss_batch_all, loss_valid) mean_acc = np.mean(acc_batch_all, axis=0) mean_loss = np.mean(loss_batch_all) num_examples = np.sum(y_valid, axis=0) num_preds = np.sum(y_pred_all, axis=0) epoch_time = time.time() - start_time print('******************************************************************************' '********************************************************') print('--------------------------------------------------------Validation, Epoch: {}' ' -----------------------------------------------------------'.format(epoch + 1)) print("Atlc\tCrdmg\tEffus\tInflt\tMass\tNodle\tPnum\tPntrx\tConsd" "\tEdma\tEmpys\tFbrss\tTkng\tHrna\t|Avg.\t|Loss\t|Run Time") for accu in mean_acc: print '{:.01%}\t'.format(accu), print '|{0:.01%}\t|{1:0.02}\t|{2}'.format(np.mean(mean_acc), mean_loss, epoch_time) for exm in num_examples: print '{:}\t'.format(exm), print("Count of pathalogies") for pred in num_preds: print '{:}\t'.format(pred), print("Count of recognized pathalogies") P = R = np.zeros((1, args.n_cls)) for cond in range(args.n_cls): y_true = y_valid[:, cond] y_pred = y_pred_all[:, cond] P[0, cond], R[0, cond] = precision_recall(y_true, y_pred) P = np.reshape(P, args.n_cls) R = np.reshape(R, args.n_cls) for p in P: print '{:0.03}\t'.format(p), print("Precision") for r in R: print '{:0.03}\t'.format(r), print("Recall") plot_precision_recall_curve(y_valid[:logits_all.shape[0], :], logits_all, epoch) write_acc_loss_csv(mean_acc, mean_loss, epoch) write_precision_recall_csv(P, R, epoch) return mean_acc, mean_loss