def predict(model): torch.cuda.empty_cache() # model.train(False) model.eval() #For data augmentation transform = {'test': transforms.ToTensor()} datasets = {} datasets = dataloader.ImageDataSet(images_dir_test, fold='test', transformation=transform['test']) print('datasets', len(datasets)) dataloaders = {} dataloaders = tdataloader(datasets, batch_size=batch_size, shuffle=True, num_workers=loader_works) print('dataloaders', len(dataloaders)) print(dataloaders) print(enumerate(dataloaders)) num_correct = 0 num_samples = 0 for data in dataloaders: files, labels, filename = data #get image,label and name #load the data to GPU / CPU image_data = Variable(files.to(device)) labels = Variable(labels.to(device)) output = model(image_data) print('output', output) _, prediction = torch.max(output.data, 1) #get predicted label print('filename', filename, '\nlabels', labels, '\nprediction', prediction) num_correct += torch.sum(prediction == labels) num_samples += prediction.size(0) acc = float(num_correct) / num_samples #compute accuracy print('acc', acc)
def training(): #For data augmentation transform = { 'train': transforms.Compose([ transforms.RandomHorizontalFlip(), transforms.ColorJitter(.3, .3, .3), transforms.ToTensor(), transforms.Normalize(mean, std) ]), 'val': transforms.Compose([ transforms.RandomHorizontalFlip(), transforms.ColorJitter(.3, .3, .3), transforms.ToTensor(), transforms.Normalize(mean, std) ]) } #Loading the dataset with train and val as keys #Data divided in training and val using separate script #From DataLoader datasets = {} datasets['train'] = dataloader.ImageDataSet( images_dir, fold='train', transformation=transform['train']) datasets['val'] = dataloader.ImageDataSet(images_dir, fold='val', transformation=transform['val']) print('datasets', len(datasets['train'])) print('datasets', len(datasets['val'])) #Dataloader.util dataloaders = {} dataloaders['train'] = tdataloader(datasets['train'], batch_size=batch_size, shuffle=True, num_workers=loader_works) dataloaders['val'] = tdataloader(datasets['val'], batch_size=batch_size, shuffle=True, num_workers=loader_works) print('dataloaders', len(dataloaders['train'])) print('dataloaders', len(dataloaders['val'])) #class labels can't be taken dirctly random needs mapping #obtaining the pretrained model model = models.resnet50(pretrained=True) #freezing other layers to finetune last one for param in model.parameters(): param.requires_grad = False num_features = model.fc.in_features print('num_features', num_features) model.fc = nn.Linear( num_features, class_labels) #handle 0 to class-1, for 25 started from 1 not 0 model = model.to(device) # print(model.classifier) # print(model) criterian = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) #getting size of train and validation datasets #training start_time = time.time() best_epoch = 0 train_loss = -1 best_loss = float('inf') best_model = None for epoch in range(1, num_epochs + 1): print('epoch {}/{}'.format(epoch, num_epochs)) for t in ['train', 'val']: num_correct = 0.0 num_samples = 0 r_loss = 0.0 if t == 'train': model.train() else: model.eval() # print('dataloaders',dataloaders[t]) for data in dataloaders[t]: # print('inner for') files, labels, filename = data files = Variable(files.to(device)) #to gpu or cpu labels = Variable(labels.to(device)) optimizer.zero_grad() #clearning old gradient from last step with torch.set_grad_enabled(t == 'train'): pred = model(files) _, prediction = torch.max(pred.data, 1) # accuracy over train and val num_correct += torch.sum(prediction == labels) num_samples += prediction.size(0) #loss computation loss = criterian(pred, labels) # if t == 'val': # print(t,'filename',filename,'\ngt labels',labels,'\nprediction',prediction) #backprop gradients at training time if t == 'train': loss.backward() optimizer.step() r_loss += loss.item( ) #for all the images- updating on batches print(t + ' epoch {}:loss {} '.format(epoch, r_loss)) if t == 'val': val_loss_track.append(r_loss) best_model = model if r_loss < best_loss: #obtain best_loss best_loss = r_loss best_model = model if t == 'train': tr_loss_track.append(r_loss) acc = float(num_correct) / num_samples #compute accuracy print(t, "accuracy : ", acc) total_time = time.time() - start_time print('Time taken', total_time) return best_model
def initialize_data(self): print('initialize_data', self.images_dir, ' ', self.images_dir_test) mean = [0.5, 0.5, 0.5] std = [0.5, 0.5, 0.5] transform = { 'train': transforms.Compose([ transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(mean, std) ]), 'val': transforms.Compose([ transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(mean, std) ]), 'test': transforms.Compose([ # transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(mean, std) ]) } # Loading the dataset with train and val as keys # Data divided in training and val using separate script datasets = {} datasets['train'] = dataloader.ImageDataSet( self.images_dir, fold='train', transformation=transform['train']) datasets['val'] = dataloader.ImageDataSet( self.images_dir, fold='val', transformation=transform['val']) datasets['test'] = dataloader.ImageDataSet( self.images_dir_test, fold='test', transformation=transform['test']) print('datasets train', len(datasets['train'])) print('datasets val', len(datasets['val'])) print('datasets test', len(datasets['test'])) # Dataloader.util self.dataloaders['train'] = tdataloader( datasets['train'], batch_size=self.batch_size_train, shuffle=True, num_workers=self.loader_works) self.dataloaders['val'] = tdataloader(datasets['val'], batch_size=self.batch_size, shuffle=True, num_workers=self.loader_works) # print('dataloaders train ', len(self.dataloaders['train'])) # print('dataloaders valida', len(self.dataloaders['val'])) self.dataloaders['test'] = tdataloader(datasets['test'], batch_size=self.batch_size, shuffle=True, num_workers=self.loader_works) # print('dataloaders test', len(self.dataloaders['test'])) print('---------------------------------------------') self.all_models = { 'vgg19': { 'lr': 0.01, 'optim': "sgd" }, 'resnet50': { 'lr': 0.0001, 'optim': "adam" }, 'resnet101': { 'lr': 0.0001, 'optim': "adam" }, 'resnet151': { 'lr': 0.0001, 'optim': "adam" }, 'googlenet': { 'lr': 0.0001, 'optim': "adam" }, 'densenet121': { 'lr': 0.0001, 'optim': "adam" }, 'un_vgg19': { 'lr': 0.01, 'optim': "sgd" }, 'un_resnet50': { 'lr': 0.0001, 'optim': "adam" }, }
def training(): mean = [0.5, 0.5, 0.5] std = [0.5, 0.5, 0.5] transform = { 'train': transforms.Compose([ transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(mean, std) ]), 'val': transforms.Compose([ transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(mean, std) ]) } #Loading the dataset with train and val as keys #Data divided in training and val using separate script datasets = {} datasets['train'] = dataloader.ImageDataSet( images_dir, fold='train', transformation=transform['train']) datasets['val'] = dataloader.ImageDataSet(images_dir, fold='val', transformation=transform['val']) print('datasets', len(datasets['train'])) print('datasets', len(datasets['val'])) #Dataloader.util dataloaders = {} dataloaders['train'] = tdataloader(datasets['train'], batch_size=64, shuffle=True, num_workers=loader_works) dataloaders['val'] = tdataloader(datasets['val'], batch_size=batch_size, shuffle=True, num_workers=loader_works) print('dataloaders train ', len(dataloaders['train'])) print('dataloaders valida', len(dataloaders['val'])) #Defining pretrained model # model = models.resnet50(pretrained=True) model = models.vgg19(pretrained=True) # for param in model.parameters(): # param.requires_grad = False #Resnet # num_features = model.fc.in_features # print('model num_features',num_features) # model.fc = nn.Linear(num_features, class_labels) # model = model.to(device) #VGG model.classifier[6] = nn.Linear(4096, class_labels) model = model.to(device) # print(model.classifier) # print(model) criterian = nn.CrossEntropyLoss() learning_rate = 0.01 # optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate, momentum=0.9) #training start_time = time.time() best_loss = float('inf') best_model = None best_acc = 0.0 best_model_wts = copy.deepcopy(model.state_dict()) for epoch in range(1, num_epochs + 1): print('epoch {}/{}'.format(epoch, num_epochs)) for t in ['train', 'val']: num_correct = 0.0 num_samples = 0 r_loss = 0.0 running_corrects = 0 if t == 'train': #training mode model.train() else: #evaluate model model.eval() count = 0 for data in dataloaders[t]: count += 1 # data has three types files, labels and filename files, labels, filename = data files = Variable(files.to(device)) #to gpu or cpu labels = Variable(labels.to(device)) optimizer.zero_grad() #clearning old gradient from last step with torch.set_grad_enabled(t == 'train'): pred = model(files) #loss computation loss = criterian(pred, labels) _, prediction = torch.max(pred, 1) #backprop gradients at training time if t == 'train': loss.backward() optimizer.step() # print(t +' iteration {}:loss {} '.format(count,r_loss)) # statistics r_loss += loss.item() * files.size(0) print(t + ' iteration {}:loss {} '.format(count, r_loss)) running_corrects += torch.sum(prediction == labels.data) epoch_loss = r_loss / len(dataloaders[t].dataset) epoch_acc = running_corrects.double() / len(dataloaders[t].dataset) print('{} Loss: {:.4f} Acc: {:.4f}'.format(t, epoch_loss, epoch_acc)) # print(t +' epoch {}:loss {} '.format(epoch,r_loss)) # deep copy the model if t == 'val' and epoch_acc > best_acc: best_acc = epoch_acc best_model_wts = copy.deepcopy(model.state_dict()) checkpoint(best_model_wts, best_loss, epoch, learning_rate) if t == 'val': val_acc_history.append(epoch_acc.item()) val_loss_track.append(epoch_loss) if t == 'train': tr_loss_track.append(epoch_loss) tr_acc_track.append(epoch_acc.item()) print() time_elapsed = time.time() - start_time print('Training complete in {:.0f}m {:.0f}s'.format( time_elapsed // 60, time_elapsed % 60)) print('Best val Acc: {:4f}'.format(best_acc)) # load best model weights model.load_state_dict(best_model_wts) #updating best model in checkpoint return model