def fit(self, img_loader): np.random.seed(24) nb = len(img_loader) nb_train = int(nb * 0.9) nb_valid = nb - nb_train indices = np.arange(nb) np.random.shuffle(indices) ind_train = indices[0:nb_train] ind_valid = indices[nb_train:] gen_train = self._build_train_generator(img_loader, indices=ind_train, batch_size=self.batch_size, shuffle=True) gen_valid = self._build_train_generator(img_loader, indices=ind_valid, batch_size=self.batch_size, shuffle=True) self.model.fit_generator( gen_train, steps_per_epoch=get_nb_minibatches(nb_train, self.batch_size), epochs=1, max_queue_size=16, workers=1, use_multiprocessing=True, validation_data=gen_valid, validation_steps=get_nb_minibatches(nb_valid, self.batch_size), verbose=1)
def fit(self, gen_builder): batch_size = 32 gen_train, gen_valid, nb_train, nb_valid =\ gen_builder.get_train_valid_generators( batch_size=batch_size, valid_ratio=0.1) self.model.fit_generator( gen_train, # Total number of steps (batches of samples) to yield from # generator before declaring one epoch finished and starting the # next epoch. It should typically be equal to the number of unique # samples of your dataset divided by the batch size. steps_per_epoch=get_nb_minibatches(nb_train, batch_size), epochs=1, # In parallel to training, a CPU process loads and preprocesses # data from disk and put it into a queue in the form of # mini-batches of size `batch_size`.`max_queue_size` controls the # maximum size of that queue. The size of the queue should be big # enough so that the training process (GPU) never # waits for data (the queue should be never be empty). # The CPU process loads chunks of 1024 images each time, and # 1024/batch_size mini-batches from that chunk are put into the # queue. Assuming training the model on those 1024/batch_size # mini-batches is slower than loading a single chunk of 1024 # images, a good lower bound for `max_queue_size` would be # (1024/batch_size). if `batch_size` is 16, you can put # `max_queue_size` to 64. max_queue_size=16, # WARNING : It is obligatory to set `workers` to 1. # This in principle controls the number of workers used # by keras to load mini-batches from disk to memory in parallel # to GPU training. But I don't like the way it works and their # code is not very commented/used, so I dont trust it that much # (we might have surprises). # The way it works in keras is by launching in parallel `workers` # threads or processes which will all use a copy of the generator # passed to `fit_generator`. So if nothing is done and `workers` # is set to some number > 1, the neural net will be trained with # repetitions of the same data, because the workers are independent # and they got through the same generator. # Hence it is necessary to introduce a shared lock between the the # processes so that they load different data, this can become a bit # complicated, so I choose to rather load exactly one chunk at a # time using 1 worker (so `workers` have to be equal to 1), but # do this single chunk loading in parallel with joblib. workers=1, use_multiprocessing=True, validation_data=gen_valid, validation_steps=get_nb_minibatches(nb_valid, batch_size), verbose=1)
def fit(self, gen_builder): batch_size = 32 gen_train, gen_valid, nb_train, nb_valid =\ gen_builder.get_train_valid_generators( batch_size=batch_size, valid_ratio=0.1) self.model.fit_generator( gen_train, steps_per_epoch=get_nb_minibatches(nb_train, batch_size), epochs=1, max_queue_size=16, workers=1, use_multiprocessing=True, validation_data=gen_valid, validation_steps=get_nb_minibatches(nb_valid, batch_size), verbose=1)
def fit(self, img_loader): # takes imaage ravels it and returns 1s where there are maxs #return self np.random.seed(24) nb = len(img_loader) nb_train = int(nb * 0.9) nb_valid = nb - nb_train indices = np.arange(nb) np.random.shuffle(indices) ind_train = indices[0:nb_train] ind_valid = indices[nb_train:] gen_train = self._build_train_generator(img_loader, indices=ind_train, batch_size=self.batch_size, shuffle=True) gen_valid = self._build_train_generator(img_loader, indices=ind_valid, batch_size=self.batch_size, shuffle=True) ''' # make sure that the memory error does not come up in generators for idx, i in enumerate(gen_train): print('train') iter(gen_train) print('valid') iter(gen_valid) print(idx) ''' print('FITTING') self.model.fit_generator( gen_train, steps_per_epoch=get_nb_minibatches(nb_train, self.batch_size), epochs=5, max_queue_size=1, workers=0, use_multiprocessing=False, validation_data=gen_valid, validation_steps=get_nb_minibatches(nb_valid, self.batch_size), verbose=100)
def predict_proba(self, img_loader): nb_test = len(img_loader) gen_test = self._build_test_generator(img_loader, self.batch_size) return self.model.predict_generator(gen_test, steps=get_nb_minibatches( nb_test, self.batch_size), max_queue_size=16, workers=1, use_multiprocessing=True, verbose=0)
def fit(self, gen_builder): batch_size = 16 nb_epochs = 1 lr = 1e-4 gen_train, gen_valid, nb_train, nb_valid =\ gen_builder.get_train_valid_generators( batch_size=batch_size, valid_ratio=0.1) self.net = self.net.cuda() net = self.net optimizer = optim.Adam(net.parameters(), lr=lr) nb_train_minibatches = get_nb_minibatches(nb_train, batch_size) nb_valid_minibatches = get_nb_minibatches(nb_valid, batch_size) criterion = nn.CrossEntropyLoss().cuda() for epoch in range(nb_epochs): t0 = time.time() net.train() # train mode nb_trained = 0 nb_updates = 0 train_loss = [] train_acc = [] for X, y in islice(gen_train, nb_train_minibatches): # convert onehot to integers, pytorch require the class indices y = y.argmax(axis=1) X = _make_variable(X) y = _make_variable(y) # zero-out the gradients because they accumulate by default optimizer.zero_grad() y_pred = net(X) loss = criterion(y_pred, y) loss.backward() # compute gradients optimizer.step() # update params # Loss and accuracy train_acc.extend(self._get_acc(y_pred, y)) train_loss.append(loss.data[0]) nb_trained += X.size(0) nb_updates += 1 if nb_updates % 100 == 0 or nb_updates == nb_train_minibatches: print( 'Epoch [{}/{}], [trained {}/{}], avg_loss: {:.4f}' ', avg_train_acc: {:.4f}'.format( epoch + 1, nb_epochs, nb_trained, nb_train, np.mean(train_loss), np.mean(train_acc))) net.eval() # eval mode nb_valid = 0 valid_acc = [] for X, y in islice(gen_valid, nb_valid_minibatches): y = y.argmax(axis=1) X = _make_variable(X) y = _make_variable(y) y_pred = net(X) valid_acc.extend(self._get_acc(y_pred, y)) nb_valid += y.size(0) delta_t = time.time() - t0 print('Finished epoch {}'.format(epoch + 1)) print('Time spent : {:.4f}'.format(delta_t)) print('Train acc : {:.4f}'.format(np.mean(train_acc))) print('Valid acc : {:.4f}'.format(np.mean(valid_acc)))
def fit(self, gen_builder): def adjust_learning_rate(optimizer, epoch, lr): if epoch == 0: lr = lr elif epoch == 1: lr = lr elif epoch == 2: lr = lr elif epoch == 3: lr = 1e-3 elif epoch == 4: lr = 1e-3 elif epoch == 5: lr = 1e-3 elif epoch == 6: lr = 1e-4 elif epoch == 7: lr = 1e-4 else: lr = 1e-4 #lr = 0.000003 for param_group in optimizer.param_groups: param_group['lr'] = lr return optimizer, lr batch_size = 8 nb_epochs = 8 lr = 1e-2 gen_train, gen_valid, nb_train, nb_valid = gen_builder.get_train_valid_generators( batch_size=batch_size, valid_ratio=0.1) self.net = self.net.cuda() net = self.net optimizer = optim.SGD(net.parameters(), lr=lr) nb_train_minibatches = get_nb_minibatches(nb_train, batch_size) nb_valid_minibatches = get_nb_minibatches(nb_valid, batch_size) criterion = nn.CrossEntropyLoss().cuda() for epoch in range(nb_epochs): optimizer, lr = adjust_learning_rate(optimizer, epoch, lr) print("using learning rate", lr) t0 = time.time() net.train() # train mode nb_trained = 0 nb_updates = 0 train_loss = [] train_acc = [] for X, y in islice(gen_train, nb_train_minibatches): y = y.argmax( axis=1 ) # convert onehot to integers, pytorch require the class indices. if (epoch % 3) == 0: #using original #if True: X = _make_variable(X) else: X = _make_variable_da(X) y = _make_variable(y) optimizer.zero_grad( ) # zero-out the gradients because they accumulate by default y_pred = net(X) loss = criterion(y_pred, y) loss.backward() # compute gradients optimizer.step() # update params # Loss and accuracy train_acc.extend(self._get_acc(y_pred, y)) train_loss.append(loss.data[0]) nb_trained += X.size(0) nb_updates += 1 if nb_updates % 100 == 0 or nb_updates == nb_train_minibatches: print( 'Epoch [{}/{}], [trained {}/{}], avg_loss: {:.4f}, avg_train_acc: {:.4f}' .format(epoch + 1, nb_epochs, nb_trained, nb_train, np.mean(train_loss), np.mean(train_acc))) net.eval() # eval mode nb_valid = 0 valid_acc = [] for X, y in islice(gen_valid, nb_valid_minibatches): y = y.argmax(axis=1) X = _make_variable(X) y = _make_variable(y) y_pred = net(X) valid_acc.extend(self._get_acc(y_pred, y)) nb_valid += y.size(0) delta_t = time.time() - t0 print('Finished epoch {}'.format(epoch + 1)) print('Time spent : {:.4f}'.format(delta_t)) print('Train acc : {:.4f}'.format(np.mean(train_acc))) print('Valid acc : {:.4f}'.format(np.mean(valid_acc))) print('Saving model') torch.save(self.net, 'model.th')
def fit(self, X, y): # torch.load('model.pt') shape = X.shape[1:] print('Computing target map...') y = np.array([gaussian_detection_map(yi, shape) for yi in y]) batch_size = 16 nb_epochs = 30 lr = 1e-3 valid_ratio = 0.05 if is_cuda: self.net = self.net.cuda() net = self.net optimizer = optim.Adam(net.parameters(), lr=lr) nb_valid = int(valid_ratio * len(X)) nb_train = len(X) - nb_valid nb_train_minibatches = get_nb_minibatches(nb_train, batch_size) criterion = nn.MSELoss() if is_cuda: criterion = criterion.cuda() for epoch in range(nb_epochs): if epoch % 10 == 0: lr /= 10 print('learning rate =', lr) t0 = time.time() net.train() # train mode nb_trained = 0 nb_updates = 0 train_loss = [] train_mae = [] train_rmse_n = [] train_err_n = [] X_train = X[:nb_train] X_valid = X[nb_train:] y_train = y[:nb_train] y_valid = y[nb_train:] for i in range(0, len(X_train), batch_size): net.train() # train mode idxs = slice(i, i + batch_size) X_minibatch = X_train[idxs] X_minibatch = self._make_X_minibatch(X_minibatch) y_minibatch = y_train[idxs] y_minibatch = _make_variable(y_minibatch) # zero-out the gradients because they accumulate by default optimizer.zero_grad() y_minibatch_pred = self._predict_map_torch(X_minibatch) loss = criterion(y_minibatch_pred, y_minibatch) loss.backward() # compute gradients optimizer.step() # update params # Loss and accuracy train_mae.append( self._get_mae_torch(y_minibatch_pred, y_minibatch)) train_rmse_n.append( self._get_rmse_n_torch(y_minibatch_pred, y_minibatch)) train_err_n.append( self._get_err_n_torch(y_minibatch_pred, y_minibatch)) train_loss.append(loss.data[0]) nb_trained += X_minibatch.size(0) nb_updates += 1 if nb_updates % 100 == 0 or nb_updates == nb_train_minibatches: print('Epoch [{}/{}], [trained {}/{}]' ', avg_loss: {:.8f}' ', avg_train_mae: {:.4f}' ', avg_train_err_n: {:.4f}' ', avg_train_rmse_n: {:.4f}'.format( epoch + 1, nb_epochs, nb_trained, nb_train, np.mean(train_loss), np.mean(train_mae), np.mean(train_err_n), np.mean(train_rmse_n))) torch.save(self.net.state_dict(), 'model.pt') net.eval() # eval mode y_valid_pred = self._predict_map(X_valid) valid_mae = self._get_mae(y_valid_pred, y_valid) valid_err_n = self._get_err_n(y_valid_pred, y_valid) valid_rmse_n = self._get_rmse_n(y_valid_pred, y_valid) np.save('x.npy', X_valid) np.save('y.npy', y_valid) np.save('y_pred.npy', y_valid_pred) delta_t = time.time() - t0 print('Finished epoch {}'.format(epoch + 1)) print('Time spent : {:.4f}'.format(delta_t)) print('Train mae : {:.4f}'.format(np.mean(train_mae))) print('Train err_n : {:.4f}'.format(np.mean(train_err_n))) print('Train rmse_n : {:.4f}'.format(np.mean(train_rmse_n))) print('Valid mae : {:.4f}'.format(np.mean(valid_mae))) print('Valid err_n : {:.4f}'.format(np.mean(valid_err_n))) print('Valid rmse_n : {:.4f}'.format(np.mean(valid_rmse_n)))