def train(train_data, test_data, net, loss, trainer, ctx, num_epochs): print("Start training on ", ctx) if isinstance(ctx, mx.Context): ctx = [ctx] for epoch in range(1, num_epochs + 1): train_loss, train_acc, n = 0.0, 0.0, 0 train_data.reset() if epoch > 10: trainer.set_learning_rate(0.001) #val_iter.reset() start = time() for batch in train_data: data, label, batch_size = _get_batch(batch, ctx) losses = [] with autograd.record(): outputs = [net(X) for X in data] losses = [loss(yhat, y) for yhat, y in zip(outputs, label)] for l in losses: l.backward() trainer.step(batch_size) train_loss += sum([l.sum().asscalar() for l in losses]) n += batch_size print("Epoch %d. Cumulative_Loss: %.3f, Loss: %.3f Time %.1f sec" % (epoch, train_loss, train_loss / n, time() - start)) if epoch % args.save_period == 0 and epoch > 1: print("saving parameters for %d" % epoch) net.collect_params().save( os.path.join("./models/", args.model_prefix + str(epoch) + ".params")) elif epoch == args.num_epochs: print("saving parameters for %d" % epoch) net.collect_params().save( os.path.join("./models/", args.model_prefix + ".params"))
def test_net(net): test_l_sum, n = 0.0, 0 for X, y in data_test_iter: Xs, ys, batch_size = utils._get_batch(X, y, ctx) y_hats = [net(Xx) for Xx in Xs] ls = [loss(y_hat, y) for y_hat, y in zip(y_hats, ys)] test_l_sum += sum([l.sum().asscalar() for l in ls]) n += sum([l.size for l in ls]) return test_l_sum / (n * 23 * 23)
def evaluate_accuracy(data_iterator, net, ctx=[mx.cpu()]): if isinstance(ctx, mx.Context): ctx = [ctx] acc = nd.array([0]) n = 0. if isinstance(data_iterator, mx.io.MXDataIter): data_iterator.reset() for batch in data_iterator: data, label, batch_size = _get_batch(batch, ctx) outputs = [net(X) for X in data] losses = [loss(yhat, y) for yhat, y in zip(outputs, label)] acc += nd.sum(losses).copyto(mx.cpu()) n += y.size acc.wait_to_read() # don't push too many operators into backend return acc.asscalar() / n
def train_net(): for epoch in range(1, num_epochs + 1): train_l_sum, n = 0.0, 0 for X, y in data_train_iter: Xs, ys, batch_size = utils._get_batch(X, y, ctx) with autograd.record(): y_hats = [net(Xx) for Xx in Xs] ls = [loss(y_hat, y) for y_hat, y in zip(y_hats, ys)] for l in ls: l.backward() trainer.step(batch_size=batchsize) train_l_sum += sum([l.sum().asscalar() for l in ls]) n += sum([l.size for l in ls]) test_loss = test_net(net=net) # l = loss(net(train_data[0]),train_data[1]) # print(l) print('epoch%d,\ttrain_loss:%f,\ttest_loss:%f' % (epoch, train_l_sum / (n * 23 * 23), test_loss))
def predict(net, data_iter): X = [] y = [] p = [] predict_loss, n = 0.0, 0.0 for batch in data_iter: data, label, batch_size = _get_batch(batch, ctx) losses = [] outputs = [net(X) for X in data] losses = [loss(yhat, y) for yhat, y in zip(outputs, label)] predict_loss += sum([l.sum().asscalar() for l in losses]) n += batch_size X.append(data[0][0].asnumpy()) y.append(label[0][0].asnumpy()) p.append(outputs[0][0].asnumpy()) print("Cumulative_Loss: %.3f, Predict_Loss: %.3f " % (predict_loss, predict_loss / n)) return X, y, p
def predict(net, data_iter): X = [] y = [] p = [] predict_loss, n = 0.0, 0.0 i = 0 for batch in data_iter: data, label, batch_size = _get_batch(batch, ctx) losses = [] outputs = [net(X) for X in data] losses = [loss(yhat, y) for yhat, y in zip(outputs, label)] predict_loss += sum([l.sum().asscalar() for l in losses]) n += batch_size data = [ denormalize(D, eval_data_scale[i * batch_size:(i + 1) * batch_size, :], axis=2) for D in data ] label = [ denormalize(D, eval_label_scale[i * batch_size:(i + 1) * batch_size, :], axis=2) for D in label ] outputs = [ denormalize(D, eval_label_scale[i * batch_size:(i + 1) * batch_size, :], axis=2) for D in outputs ] outputs = [ D.reshape((batch_size, 1, sequence_length)) for D in outputs ] X.append(data[0][0].asnumpy()) y.append(label[0][0].asnumpy()) p.append(outputs[0][0].asnumpy()) i += 1 print("Cumulative_Loss: %.3f, Predict_Loss: %.3f " % (predict_loss, predict_loss / n)) return X, y, p
def verify(self, path_root, show_image=False): aug = mx.image.CreateAugmenter(data_shape=self.dataShape, resize=self.stdSize, mean=True, std=True) dataIter = mx.image.ImageIter(batch_size=1, data_shape=self.dataShape, label_width=self.outputNum, path_imglist=os.path.join( path_root, 'landmarks.lst'), path_root=path_root, aug_list=aug) n = 0 acc = nd.array([0]) for batch in dataIter: #pdb.set_trace() data, label, batch_size = utils._get_batch(batch, [self.ctx]) for X, y in zip(data, label): y = y.astype('float32') y0 = self.net(X) acc += nd.sum((y0 - y) * (y0 - y)).copyto(mx.cpu()) n += y.shape[0] if show_image: img = X.asnumpy()[0] for k in range(3): img[k, :, :] = img[k, :, :] * self.std[k] + self.mean[ k] #restore mean/std img = np.transpose(img, (1, 2, 0)) img = cv2.cvtColor(np.uint8(img), cv2.COLOR_BGR2GRAY) img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) img = cv2.resize(img, (img.shape[1] * 1, img.shape[0] * 1)) for k in range(0, self.outputNum, 2): x, y = y0.asnumpy()[0, k], y0.asnumpy()[0, k + 1] x, y = np.int64(x * img.shape[1]), np.int64( y * img.shape[0]) cv2.circle(img, (x, y), 3, (128, 255, 128)) cv2.imshow("src", np.uint8(img)) cv2.waitKey(-1) acc.wait_to_read() print 'total ', n print 'acc = ', acc.asscalar() / (2 * n)
def main(_): (train_addrs, train_labels, val_addrs, val_labels, test_addrs, test_labels) = utils.adressLabelSort('sortedTestAudio2') addr = train_addrs embedding_labels = train_labels print('number of addr: ', len(addr)) print('number of labels: ', len(embedding_labels)) (examples_batch, embedding_labels) = utils._get_batch(addr, embedding_labels) tfrecords_filename = 'Evalval1.tfrecords' writer = tf.python_io.TFRecordWriter(tfrecords_filename) # restricting memory usage, TensorFlow is greedy and will use all memory otherwise config = tf.ConfigProto() #config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allocator_type = 'BFC' #config.gpu_options.allow_growth = True config.gpu_options.per_process_gpu_memory_fraction = 0.90 with tf.Graph().as_default(), tf.Session(config=config) as sess: vggish_slim.define_vggish_slim( training=False) # Defines the VGGish TensorFlow model. vggish_slim.load_vggish_slim_checkpoint( sess, 'vggish_model.ckpt' ) # Loads a pre-trained VGGish-compatible checkpoint. # locate input and output tensors. features_tensor = sess.graph.get_tensor_by_name( vggish_params.INPUT_TENSOR_NAME) embedding_tensor = sess.graph.get_tensor_by_name( vggish_params.OUTPUT_TENSOR_NAME) feed_dict = {features_tensor: examples_batch} [embedding_batch] = sess.run([embedding_tensor], feed_dict=feed_dict) print('example_batch shape: ', examples_batch.shape) print('embedding_batch shape: ', embedding_batch.shape) print('labels_batch shape: ', len(embedding_labels)) # store the data to the TFRecords file. for i in range(len(embedding_batch)): embedding = embedding_batch[i] # convert into proper data type: embedding_length = embedding_labels[i] # embedding.shape[0] embedding_raw = embedding.tostring() # Create a feature feature = { 'Evalval1/labels': utils._int64_feature(embedding_length), 'Evalval1/embedding': utils._bytes_feature(embedding_raw) } # Create an example protocol buffer example = tf.train.Example(features=tf.train.Features( feature=feature)) # Serialize to string and write on the file writer.write(example.SerializeToString()) writer.close() sys.stdout.flush()
parser = argparse.ArgumentParser() parser.add_argument('--batch_size', default=256, type=int) args = parser.parse_args() print(args) ctx = mx.gpu(0) capnet = CapsNet(args.batch_size, ctx) capnet.load_params('capnet.params', ctx) recnet = ReconNet(args.batch_size, ctx) recnet.load_params('recnet.params', ctx) train_data, test_data = utils.load_data_mnist(batch_size=args.batch_size,resize=28) sum_capout = mx.nd.zeros((16, 10), ctx) sum_label = mx.nd.zeros((10), ctx) for i, batch in enumerate(test_data): data, label = utils._get_batch(batch, ctx) one_hot_label = nd.one_hot(label, 10) capout = capnet(data) # maybe I should not use label to create mask masked_capoutput = capout * nd.expand_dims(one_hot_label, axis=1) sum_capout += nd.sum(masked_capoutput, axis=0) sum_label += nd.sum(one_hot_label, axis=0) recoutput = recnet(masked_capoutput) rec_x = vis_square(recoutput.asnumpy().reshape(-1,28,28)) # uncomment to plot rec if i == 0: input_x = vis_square(data.asnumpy().reshape(-1,28,28)) # cv2.imwrite('rec/input_x.png', (input_x*255).astype(int)) # cv2.imwrite('rec/rec_x.png', (rec_x*255).astype(int)) mean_capout = sum_capout / sum_label
'learning_rate': args.init_lr, 'wd': args.weight_decay }, ) print("Start training...") for epoch in range(args.epochs): start_time = timeit.default_timer() # setting the learning rate lr_ = utils.lr_poly(args.init_lr, epoch, args.epochs, 0.9) train_l_sum, train_acc_sum, n, m, start = 0.0, 0.0, 0, 0, timeit.default_timer( ) for i, batch in enumerate(trainloader): imgBatchList, lblBatchList, batch_size = utils._get_batch( batch, ctx) ls = [] with autograd.record(): y_hats = [model(img) for img in imgBatchList] ls = [loss(y_hat, y) for y_hat, y in zip(y_hats, lblBatchList)] for l in ls: l.backward() trainer.step(batch_size) train_l_sum += sum([l.sum().asscalar() for l in ls]) n += sum([l.size for l in ls]) train_acc_sum += sum([(y_hat.argmax(axis=1) == y).sum().asscalar() for y_hat, y in zip(y_hats, lblBatchList)]) m += sum([y.size for y in lblBatchList]) test_acc = utils.evaluate_accuracy(testloader, model, ctx)