def preprocess(self, img, augs): data = mx.nd.array(img).astype('float32').as_in_context(self.ctx) for aug in augs: data = aug(data) data = mx.nd.transpose(data, (2, 0, 1)) data = color_normalize(data / 255, self.mean, self.std) return data
def train_util(net, train_iter, validation_iter, loss_fn, trainer, ctx, epochs, batch_size): metric = mx.metric.create(['acc']) for epoch in range(epochs): for i, (data, label) in enumerate(train_iter): st = time.time() # ensure context data = data.as_in_context(ctx) print(data.shape) label = label.as_in_context(ctx) # normalize images data = color_normalize(data/255, mean=mx.nd.array([0.485, 0.456, 0.406]).reshape((1,3,1,1)), std=mx.nd.array([0.229, 0.224, 0.225]).reshape((1,3,1,1))) with autograd.record(): output = net(data) loss = loss_fn(output, label) loss.backward() trainer.step(data.shape[0]) # Keep a moving average of the losses metric.update([label], [output]) names, accs = metric.get() print('[Epoch %d Batch %d] speed: %f samples/s, training: %s'%(epoch + 1, i + 1, batch_size/(time.time()-st), metric_str(names, accs))) # if i%100 == 0: # net.collect_params().save('./checkpoints/%d-%d.params'%(epoch, i)) train_acc = evaluate_accuracy(train_iter, net) validation_acc = evaluate_accuracy(validation_iter, net) print("Epoch %s | training_acc %s | val_acc %s " % (epoch + 1, train_acc, validation_acc))
def train(net, train_iter, val_iter, epochs, ctx): if isinstance(ctx, mx.Context): ctx = [ctx] trainer = gluon.Trainer(net.collect_params(), 'sgd', { 'learning_rate': learning_rate, 'wd': wd }) loss = gluon.loss.SoftmaxCrossEntropyLoss() # best_f1 = 0 val_names, val_accs = evaluate(net, val_iter, ctx) logging.info('[Initial] validation: %s' % (metric_str(val_names, val_accs))) for epoch in range(epochs): tic = time.time() train_iter.reset() btic = time.time() for i, batch in enumerate(train_iter): # the model zoo models expect normalized images print(batch.data[0]) data = color_normalize(batch.data[0] / 255, mean=mx.nd.array([0.485, 0.456, 0.406]).reshape( (1, 3, 1, 1)), std=mx.nd.array([0.229, 0.224, 0.225]).reshape( (1, 3, 1, 1))) data = gluon.utils.split_and_load(data, ctx_list=ctx, batch_axis=0) label = gluon.utils.split_and_load(batch.label[0], ctx_list=ctx, batch_axis=0) outputs = [] Ls = [] with autograd.record(): for x, y in zip(data, label): z = net(x) # rescale the loss based on class to counter the imbalance problem L = loss(z, y) * ( 1 + y * positive_class_weight) / positive_class_weight # store the loss and do backward after we have done forward # on all GPUs for better speed on multiple GPUs. Ls.append(L) outputs.append(z) for L in Ls: L.backward() trainer.step(batch.data[0].shape[0]) metric.update(label, outputs) if log_interval and not (i + 1) % log_interval: names, accs = metric.get() logging.info( '[Epoch %d Batch %d] speed: %f samples/s, training: %s' % (epoch, i, batch_size / (time.time() - btic), metric_str(names, accs))) btic = time.time() names, accs = metric.get() metric.reset() logging.info('[Epoch %d] training: %s' % (epoch, metric_str(names, accs)))
def img_normalization(img): # assert img.sum().asscalar() != 224 * 224 * 255 * 3 img = img.astype('float32') / 255 normalized_img = image.color_normalize(img, mean=nd.array([0.485, 0.456, 0.406]), std=nd.array([0.229, 0.224, 0.225])) return normalized_img
def train_util(net, train_iter, val_iter, loss_fn, trainer, ctx, epochs, checkpoint_dir, init_epoch=0): ''' Params: - net: network to train - train_iter: gluon.data.DataLoader with the training data - val_iter: " " validation data - loss_fn: loss function to use for training - trainer: gluon.Trainer to use for training - ctx: context where we will operate (GPU or CPU) - epochs: number of epochs to train for - batch_size - checkpoint_dir: directory where checkpoints are saved every 100 batches - init_epoch: set to the initial epoch in case training is resumed from a previous execution''' batch_size = train_iter._batch_sampler._batch_size res = {'train':[],'val':[]} for epoch in range(1+init_epoch, epochs+init_epoch+1): metric = mx.metric.create(['acc']) for i, (data, label) in enumerate(train_iter): st = time.time() # ensure context data = data.as_in_context(ctx) label = label.as_in_context(ctx) # normalize images data = color_normalize(data/255, mean, std) with mx.autograd.record(): output = net(data) loss = loss_fn(output, label) loss.backward() trainer.step(data.shape[0], ignore_stale_grad=True) # Keep a moving average of the losses metric.update([label], [output]) names, accs = metric.get() if i%50 == 0: print('[Epoch %d Batch %d] speed: %f samples/s, training: %s'%(epoch, i, batch_size/(time.time()-st), metric_str(names, accs))) if i%200 == 0: # Store accuracy and reset the metric metric.reset() if i != 0: res['train'].append(accs) names, train_acc = metric.get() val_acc = evaluate_accuracy(val_iter, net) # Only save model params if results are better than the previous ones if res['val'] and val_acc > max(res['val']): net.save_params('%s/%d.params'%(checkpoint_dir, epoch)) res['train'].append(train_acc) res['val'].append(val_acc) print("Epoch %s | training_acc %s | val_acc %s " % (epoch, train_acc, val_acc)) return res
def evaluate_accuracy(data_iterator, net): acc = mx.metric.Accuracy() for i, (data, label) in enumerate(data_iterator): data = data.as_in_context(ctx) label = label.as_in_context(ctx) data = color_normalize(data/255, mean, std) output = net(data) prediction = mx.nd.argmax(output, axis=1) acc.update(preds=prediction, labels=label) return acc.get()[1]
def __getitem__(self, index): img = image.imread(self.img_age_list[index][0], to_rgb=True) age = self.img_age_list[index][1] img = image.color_normalize(img.astype('float32') / 255, mean=nd.array([0.485, 0.456, 0.406]), std=nd.array([0.229, 0.224, 0.225])) img = image.imresize(img, 224, 224) img = nd.transpose(img, (2, 0, 1)) age = nd.array([age]).asscalar().astype('float32') return img, age
def load_image(img_path, long_side_length): x = image.imread(img_path) x = image.resize_short(x, long_side_length) x, _ = image.center_crop(x, (448, 448)) x = x.astype('float32') x = x / 255 x = image.color_normalize(x, mean=nd.array([0.485, 0.456, 0.406]), std=nd.array([0.229, 0.224, 0.225])) x = x.reshape((1, 3, 448, 448)) return x
def evaluate_accuracy(data_iterator, net): acc = mx.metric.Accuracy() for i, (data, label) in enumerate(data_iterator): data = data.as_in_context(ctx) label = label.as_in_context(ctx) data = color_normalize(data/255, mean=mx.nd.array([0.485, 0.456, 0.406]).reshape((1,3,1,1)), std=mx.nd.array([0.229, 0.224, 0.225]).reshape((1,3,1,1))) output = net(data) prediction = nd.argmax(output, axis=1) acc.update(preds=prediction, labels=label) return acc.get()[1]
def color_normalize(src, mean, std=None): """ Normalize src with mean and std. :param src : NDArray Input image :param mean : NDArray RGB mean to be subtracted :param std : NDArray RGB standard deviation to be divided :return: NDArray An `NDArray` containing the normalized image. """ src = src.astype(np.float32) return img.color_normalize(src, mean, std)
def predict_class(net, img): # with open(fname, 'rb') as f: # img = image.imdecode(f.read()) data, _ = transform(img, -1, test_augs) # plt.imshow(data.transpose((1,2,0)).asnumpy()/255) data = data.expand_dims(axis=0) data = color_normalize(data / 255, mean=mx.nd.array([0.485, 0.456, 0.406]).reshape( (1, 3, 1, 1)), std=mx.nd.array([0.229, 0.224, 0.225]).reshape( (1, 3, 1, 1))) out = net(data.as_in_context(mx.cpu())) # plt.imshow(img.asnumpy()) pred, label = get_label_and_prod(out) return label
def predict(net, url, label1, label0, show_img=False): data = read_image(url) data = data.expand_dims(axis=0) data = color_normalize(data / 255, mean=nd.array([0.485, 0.456, 0.406]).reshape( (1, 3, 1, 1)), std=nd.array([0.229, 0.224, 0.225]).reshape( (1, 3, 1, 1))) out = softmax(net(data.as_in_context(mx.cpu()))) prediction = [label0, label1][int(nd.argmax(out, axis=1).asscalar())] print('Probability of {}: {}'.format(label1, out[0][1].asscalar())) print('Probability of {}: {}'.format(label0, out[0][0].asscalar())) print('That image (probably) contained {}.\n'.format(prediction)) if show_img: plt.imshow(img / 255) plt.subplot(1, 2, 2) plt.show()
def evaluate(net, data_iter, ctx): data_iter.reset() for batch in data_iter: data = color_normalize(batch.data[0] / 255, mean=mx.nd.array([0.485, 0.456, 0.406]).reshape( (1, 3, 1, 1)), std=mx.nd.array([0.229, 0.224, 0.225]).reshape( (1, 3, 1, 1))) data = gluon.utils.split_and_load(data, ctx_list=ctx, batch_axis=0) label = gluon.utils.split_and_load(batch.label[0], ctx_list=ctx, batch_axis=0) outputs = [] for x in data: outputs.append(net(x)) metric.update(label, outputs) out = metric.get() metric.reset() return out
def color_normalize(src, mean, std=None): """Normalize src with mean and std. Parameters ---------- src : NDArray Input image mean : NDArray RGB mean to be subtracted std : NDArray RGB standard deviation to be divided Returns ------- NDArray An `NDArray` containing the normalized image. """ src = src.astype(np.float32) return img.color_normalize(src, mean, std)
def evaluate(net, data_iter, ctx): data_iter.reset() print('inside of evaluate function:') for i, batch in enumerate(data_iter): print('batch%d' % i) data = color_normalize(batch.data[0] / 255, mean=mx.nd.array([0.485, 0.456, 0.406]).reshape( (1, 3, 1, 1)), std=mx.nd.array([0.229, 0.224, 0.225]).reshape( (1, 3, 1, 1))) data = gluon.utils.split_and_load(data, ctx_list=ctx, batch_axis=0) label = gluon.utils.split_and_load(batch.label[0], ctx_list=ctx, batch_axis=0) outputs = [] for i, x in enumerate(data): print('x number %d' % i) outputs.append(net(x)) metric.update(label, outputs) out = metric.get() metric.reset() return out
log_interval = 100 # val_names, val_accs = evaluate(dognet, val_iter, ctx) # print('[Initial] validation: %s'%(metric_str(val_names, val_accs))) for epoch in range(epochs): print('epoch #', epoch) tic = time.time() train_iter.reset() btic = time.time() for i, batch in enumerate(train_iter): print('batch #:', i) # the model zoo models expect normalized images data = color_normalize(batch.data[0] / 255, mean=mx.nd.array([0.485, 0.456, 0.406]).reshape( (1, 3, 1, 1)), std=mx.nd.array([0.229, 0.224, 0.225]).reshape( (1, 3, 1, 1))) data = gluon.utils.split_and_load(data, ctx_list=ctx, batch_axis=0) label = gluon.utils.split_and_load(batch.label[0], ctx_list=ctx, batch_axis=0) outputs = [] Ls = [] with autograd.record(): for x, y in zip(data, label): z = dognet(x) # rescale the loss based on class to counter the imbalance problem L = unbalanced_loss(loss, z, y) # store the loss and do backward after we have done forward # on all GPUs for better speed on multiple GPUs.
def preprocess(img, image_shape): img = img.astype('float32') / 255 img = image.imresize(img, *image_shape) img = image.color_normalize(img, rgb_mean, rgb_std) return img.transpose((2, 0, 1)).expand_dims(axis=0)
def img_norm(img, mean, std): #img is a ndarray img = F.array(img) / 255. return mx_img.color_normalize(img, F.array(mean), F.array(std))