def train_model(clf: CnnClassifier, results_file: TextIO) -> TrainedModel: ''' Trains a CNN classifier. Computes the accuracy on the validation set. Returns both the trained classifier and accuracy. ''' results_file.write('epoch,train_loss,train_loss_sd,val_accuracy\n') n_epochs = 100 for i in range(n_epochs): print(f"epoch {i+1}") # reset list of per-epoch training loss loss_train = [] for batch in train_batches: # train classifier loss_train.append(clf.train(batch.data, batch.label)) # output as requested loss_train = np.array(loss_train) print(f" train loss: {np.mean(loss_train):5.3f} +- " f"{np.std(loss_train):5.3f}") # calculate validation accuracy accuracy = Accuracy() for batch in val_batches: # predict and update accuracy prediction = clf.predict(batch.data) accuracy.update(prediction, batch.label) print(f" val acc: {accuracy}") results_file.write(f"{i},{np.mean(loss_train)},") results_file.write(f"{np.std(loss_train)},{accuracy.accuracy()}\n") return TrainedModel(clf, accuracy)
add(-127.5), mul(1 / 127.5), rcrop(25, 2, 'median'), hwc2chw() ]) batchGenerator_training = BatchGenerator(pets_training, BATCH_SIZE, shuffle=True, op=op_chain) batchGenerator_validation = BatchGenerator(pets_validation, BATCH_SIZE, shuffle=False, op=op_chain) clf = CnnClassifier(net, (BATCH_SIZE, NUM_CHANNELS, IMAGE_HEIGHT, IMAGE_WIDTH), NUM_CLASSES, lr, wd) loss_list = [] best_accuracy = 0.0 accuracy = Accuracy() epochs_since_best_accuracy = 0 for epoch in range(0, EPOCHS): print("Epoche: ", epoch + 1) for batch in batchGenerator_training: loss = clf.train(batch.data, batch.label) loss_list.append(loss) loss = np.array(loss_list) loss_mean = np.mean(loss) loss_deviation = np.std(loss) print("Train loss: ", loss_mean, "-+", loss_deviation)
# Apply block 2followed by max pool x = self.relu(self.conv2_1(x)) x = self.relu(self.conv2_2(x)) x = self.pool(x) # Prepare the image for the fully connected layer x = x.view(-1, int(64 * 32 / 2 / 2 * 32 / 2 / 2)) # Apply the fully connected layer and return the result return self.fc(x) img_shape = train_data.image_shape() num_classes = train_data.num_classes() net = Net(img_shape, num_classes) clf = CnnClassifier(net, (0, *img_shape), num_classes, 0.01, 0.01) not_improved_since = 0 best_accuracy = 0 stop_epoch = 0 for epoch in range(100): losses = [] for batch in train_batches: loss = clf.train(batch.data, batch.label) losses.append(loss) losses = np.array(losses) mean = round(np.mean(losses), 3) std = round(np.std(losses), 3) print("epoch {}".format(epoch)) print(" train loss: {} +- {}".format(mean, std))
# transfer to CUDA (if available) if torch.cuda.is_available(): pretrainedModel.cuda() netTransfer = CatsDogsModelTransfer(pretrainedModel, 512) # Learning rate to use lr = 0.005 # weight decay to use wd = 0.001 # Step 4: wrap into CnnClassifier net = CatsDogsModel() # check whether GPU support is available if torch.cuda.is_available(): net.cuda() clf = CnnClassifier(net, (0, 3, 32, 32), train_data.num_classes(), lr, wd) # check whether GPU support is available if torch.cuda.is_available(): netTransfer.cuda() clfTransfer = CnnClassifier( netTransfer, (0, 3, 32, 32), train_data.num_classes(), lr, wd ) # Step 5: train in 100 epochs def train_model(clf: CnnClassifier, results_file: TextIO) -> TrainedModel:
def forward(self, x): # run the steps ... return self.layers.forward(x) # Learning rate to use lr = 0.0001 # weight decay to use wd = 0.0 # Step 4: wrap into CnnClassifier net = CatsDogsModel() # check whether GPU support is available if torch.cuda.is_available(): net.cuda() clf = CnnClassifier(net, (0, 3, 32, 32), train_data.num_classes(), lr, wd) netSimple = CatsDogsModelSimple() # check whether GPU support is available if torch.cuda.is_available(): netSimple.cuda() clfSimple = CnnClassifier(netSimple, (0, 3, 32, 32), train_data.num_classes(), lr, wd) netComplex = CatsDogsModelComplex() # check whether GPU support is available if torch.cuda.is_available(): netComplex.cuda() clfComplex = CnnClassifier(netComplex, (0, 3, 32, 32), train_data.num_classes(), lr, wd)
def train(lr, wd, operation): print("Training a network with:") print("Weight Decay = {}".format(wd)) print("Augmentation = {}".format(operation)) print("Learning Rate = {}".format(lr)) device = torch.device("cuda" if CUDA else "cpu") img_shape = train_data.image_shape() num_classes = train_data.num_classes() net = Net(img_shape, num_classes).to(device) clf = CnnClassifier(net, (0, *img_shape), num_classes, lr, wd) op = operations[operation] train_batches = BatchGenerator(train_data, 128, False, op) val_batches = BatchGenerator(val_data, 128, False, op) not_improved_since = 0 best_accuracy = 0 best_loss = 0 stop_epoch = 0 for epoch in range(NR_EPOCHS): print("Epoch {}/{}".format(epoch, NR_EPOCHS), end="\r") losses = [] for batch in train_batches: loss = clf.train(batch.data, batch.label) losses.append(loss) losses = np.array(losses) mean = round(np.mean(losses), 3) std = round(np.std(losses), 3) accuracy = Accuracy() for batch in val_batches: predictions = clf.predict(batch.data) accuracy.update(predictions, batch.label) acc = round(accuracy.accuracy(), 3) # Early stopping if acc > best_accuracy: stop_epoch = epoch not_improved_since = 0 best_accuracy = acc best_loss = mean else: not_improved_since += 1 if not_improved_since > EARLY_STOPPING: # if not improved since 5 epochs stop training break print() print("Best val accuracy after epoch {}".format(stop_epoch + 1)) print("Validation Accuracy: {}".format(best_accuracy)) print("Train Loss: {}".format(best_loss)) with open(RESULTS_FILE, "a") as file: file.write("Trained a network with:\n") file.write("Weight Decay = {}\n".format(wd)) file.write("Augmentation = {}\n".format(operation)) file.write("Learning Rate = {}\n".format(lr)) file.write("---\n") file.write("Best val accuracy after epoch {}\n".format(stop_epoch + 1)) file.write("Validation Accuracy: {}\n".format(best_accuracy)) file.write("Train Loss: {}\n".format(best_loss)) file.write("\n#################################\n")
# Apply conv followd by relu, then in next line pool x = self.relu(self.conv1(x)) x = self.pool(x) # Apply conv followd by relu, then in next line pool x = self.relu(self.conv2(x)) x = self.pool(x) # Prepare the image for the fully connected layer x = x.view(-1, int(10 * 32 / 2 / 2 * 32 / 2 / 2)) # Apply the fully connected layer and return the result return self.fc(x) net = Net(2) classifier = CnnClassifier(net, (0, 32, 32, 3), 2, 0.01, 0.01) for batch in train_batches: image_0 = reverse_op(batch.data[0]) image_1 = reverse_op(batch.data[1]) loss = classifier.train(batch.data, batch.label) print(loss, end="\r") break print() # show the first image item = pets_train.__getitem__(0) cv2.imshow('Test Image', item.data) cv2.waitKey(0) # waits until a key is pressed cv2.destroyAllWindows() # destroys the window showing image