def binary_output(dataloader): embedding_net = EmbeddingResnet() net = TripletNet(embedding_net) #net = SiameseNet(embedding_net) #net = ClassificationNet(embedding_net, 100) net.load_state_dict(torch.load('./model/%s' % args.pretrained)) use_cuda = torch.cuda.is_available() if use_cuda: net.cuda() full_batch_output = torch.cuda.FloatTensor() full_batch_label = torch.cuda.LongTensor() net.eval() for batch_idx, (inputs, targets) in enumerate(dataloader): if use_cuda: inputs, targets = inputs.cuda(), targets.cuda() inputs, targets = Variable(inputs, volatile=True), Variable(targets) outputs = net.get_embedding(inputs).data full_batch_output = torch.cat((full_batch_output, outputs.data), 0) full_batch_label = torch.cat((full_batch_label, targets.data), 0) return torch.round(full_batch_output), full_batch_label
def main(): import torch from torch.optim import lr_scheduler import torch.optim as optim from torch.autograd import Variable from trainer import fit import numpy as np cuda = torch.cuda.is_available() # Training settings parser = argparse.ArgumentParser(description='PyTorch MNIST Example') parser.add_argument('--batch-size', type=int, default=100, metavar='N', help='input batch size for training (default: 64)') parser.add_argument('--test-batch-size', type=int, default=100, metavar='N', help='input batch size for testing (default: 1000)') parser.add_argument('--epochs', type=int, default=100, metavar='N', help='number of epochs to train (default: 10)') parser.add_argument('--lr', type=float, default=0.001, metavar='LR', help='learning rate (default: 0.01)') parser.add_argument('--momentum', type=float, default=0.5, metavar='M', help='SGD momentum (default: 0.5)') parser.add_argument('--no-cuda', action='store_true', default=False, help='disables CUDA training') parser.add_argument('--seed', type=int, default=1, metavar='S', help='random seed (default: 1)') parser.add_argument('--log-interval', type=int, default=10, metavar='N', help='how many batches to wait before logging training status') parser.add_argument('--save-model', action='store_true', default=True, help='For Saving the current Model') args = parser.parse_args() use_cuda = not args.no_cuda and torch.cuda.is_available() torch.manual_seed(args.seed) np.random.seed(args.seed) torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False device = torch.device("cuda" if use_cuda else "cpu") kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {} from datetime import datetime import os loging = False ismultitask = False loso = False if (args.save_model): model_save_path = 'model/triplet/' if not os.path.isdir(model_save_path): os.makedirs(model_save_path) if loging: fname = model_save_path + datetime.today().strftime("%m_%d_%H_%M") + ".txt" f = open(fname, 'w') x_data, y_data = load_smt() y_subj = np.zeros([108, 200]) for i in range(108): y_subj[i, :] = i * 2 y_subj = y_data.reshape(108, 200) + y_subj y_subj = y_subj.reshape(21600) # nonbciilli = np.s_[0,1,2,4,5,8,16,17,18,20,21,27,28,29,30,32,35,36,38,42,43,44,51] valtype = 'sess' if valtype == 'loso': pass elif valtype == 'sess': from networks import EmbeddingDeep4CNN, TripletNet, FineShallowCNN, EmbeddingDeepCNN, QuintupletNet, EmbeddingShallowCNN from losses import TripletLoss_dev2 # make model for metric learning margin = 1 embedding_net = EmbeddingShallowCNN() # clf_net = nn.Sequential(EmbeddingDeep4CNN(),nn.Linear(1000,2),nn.Dropout(p=1),nn.LogSoftmax(dim=1)) print(embedding_net) model = TripletNet(embedding_net) if cuda: model.cuda() loss_fn = TripletLoss_dev2(margin).cuda() n_epochs =1 log_interval = 10 load_model_path = None#'C:\\Users\dk\PycharmProjects\giga_cnn\구모델\\clf_83_8.pt'#'clf_29.pt' #'triplet_mg26.pt'#'clf_triplet2_5.pt' #'triplet_31.pt' model.fc = nn.Sequential(nn.Linear(1000, 2), nn.Dropout(p=0.5)) if load_model_path is not None: embedding_net.load_state_dict(torch.load(load_model_path)) # For classification dataset_train = GigaDataset(x=x_data, y=y_data, valtype=valtype, istrain=True, sess=1) train_loader = torch.utils.data.DataLoader(dataset_train, batch_size=args.batch_size, shuffle=True, **kwargs) dataset_test = GigaDataset(x=x_data, y=y_data, valtype=valtype, istrain=False, sess=2, subj=-1) test_loader = torch.utils.data.DataLoader(dataset_test, batch_size=args.batch_size, shuffle=False, **kwargs) #make model for classification newmodel = nn.Sequential(model.embedding_net, nn.Linear(1000, 2), nn.Dropout(p=0.5), nn.LogSoftmax(dim=1) ).to(device) print(newmodel) newmodel.to(device) optimizer = optim.SGD(newmodel.parameters(), lr=0.001, momentum=0.9) # optimizer = optim.Adam(newmodel.parameters()) for epoch in range(0): train(args, newmodel, device, train_loader, optimizer, epoch) j_loss, j_score = eval(args, newmodel, device, test_loader) # For embedding triplet_dataset_train = TripletGiga(x=x_data, y=y_data,valtype=valtype, istrain=True, sess=1) triplet_train_loader = torch.utils.data.DataLoader(triplet_dataset_train, batch_size=args.batch_size, shuffle=True, **kwargs) triplet_dataset_test = TripletGiga(x=x_data, y=y_data,valtype=valtype, istrain=False, sess=2, subj=-1) triplet_test_loader = torch.utils.data.DataLoader(triplet_dataset_test, batch_size=args.batch_size, shuffle=False, **kwargs) optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9) scheduler = lr_scheduler.StepLR(optimizer, 8, gamma=1, last_epoch=-1) from sklearn.pipeline import Pipeline from sklearn.discriminant_analysis import LinearDiscriminantAnalysis from sklearn.model_selection import ShuffleSplit, cross_val_score lda = LinearDiscriminantAnalysis() Testmodel = nn.Sequential(model.embedding_net, model.fc, nn.LogSoftmax(dim=1)).to(device) # tempEmbeddingNet = nn.Sequential(model.embedding_net, # nn.Linear(1000,1000), # nn.Sigmoid()) # model = TripletNet(embedding_net) print(model) for temp in range(1, 30): # 10epoch마다 세이브 fit(triplet_train_loader, triplet_test_loader, model, loss_fn, optimizer, scheduler, n_epochs, cuda, log_interval) j_loss, j_score = eval(args, Testmodel, device, train_loader) j_loss, j_score = eval(args, Testmodel, device, test_loader) torch.save(model.state_dict(), 'clf_' + str(temp) + '.pt') torch.save(model.state_dict(), 'shallowDG_150epoch_82acc' + str(temp) + '.pt') #for visualization dataset_train_subj = GigaDataset(x=x_data, y=y_subj, valtype=valtype, istrain=True, sess=1) train_loader_subj = torch.utils.data.DataLoader(dataset_train_subj, batch_size=args.batch_size, shuffle=True, **kwargs) dataset_test_subj = GigaDataset(x=x_data, y=y_subj, valtype=valtype, istrain=False, sess=2, subj=-1) test_loader_subj = torch.utils.data.DataLoader(dataset_test_subj, batch_size=args.batch_size, shuffle=False, **kwargs) # train_embeddings_tl, train_labels_tl = extract_features(train_loader_subj.dataset, model.embedding_net.convnet,0,100) # val_embeddings_tl, val_labels_tl = extract_embeddings(test_loader_subj, model.embedding_net, 1000) train_embeddings_tl, train_labels_tl = extract_embeddings(train_loader_subj, model.embedding_net,1000) val_embeddings_tl, val_labels_tl = extract_embeddings(test_loader_subj, model.embedding_net,1000) # # train_embeddings_tl, train_labels_tl = extract_embeddings(train_loader_subj, model.embedding_net.convnet[0], 1000) # val_embeddings_tl, val_labels_tl = extract_embeddings(test_loader_subj, model.embedding_net, 1000) # = train_labels_tl-train_labels_tl%2 # from torchvision import datasets, models, transforms # temp = model.embedding_net.children() # newmodel = torch.nn.Sequential(*(list(model.embedding_net.children())[:])) # for param in model.embedding_net.parameters(): # param.requires_grad = True #newembedding_net = torch.nn.Sequential(*(list(model.embedding_net.children())[:])) # from sklearn.manifold import TSNE tsne = TSNE(n_components=2,perplexity=30) #features = np.concatenate([train_embeddings_tl,val_embeddings_tl]) #val_labels_tl = val_labels_tl+2 #labels = np.concatenate([train_labels_tl,val_labels_tl]) train_tsne = tsne.fit_transform(train_embeddings_tl[0:2000]) # plot_embeddings(train_tsne,train_labels_tl[0:1000]) plot_features(train_tsne,train_labels_tl[0:2000]) plot_features3d(train_tsne,train_labels_tl[0:1000]%2) val_tsne = tsne.fit_transform(val_embeddings_tl) plot_embeddings(val_tsne, (val_labels_tl-108)-(val_labels_tl-108)%2) for param in model.embedding_net.parameters(): param.requires_grad = True #embedding_net2 = EmbeddingDeep4CNN() newmodel = nn.Sequential(model.embedding_net, nn.Linear(1000, 2), nn.Dropout(p=0.5), nn.LogSoftmax(dim=1), ).to(device) print(newmodel) #newmodel.fc_lr = nn.Linear(1000,2) newmodel.to(device) optimizer = optim.SGD(newmodel.parameters(), lr=0.001, momentum=0.9) #optimizer = optim.Adam(newmodel.parameters()) for epoch in range(1, 100): train(args, newmodel, device, train_loader, optimizer, epoch) j_loss, j_score = eval(args, newmodel, device, test_loader) if args.save_model: torch.save(newmodel.state_dict(),'clf_83_8.pt') newmodel.load_state_dict(torch.load(load_model_path)) # Visualize feature maps activation = {} def get_activation(name): def hook(model, input, output): activation[name] = output.detach() return hook handle = model.embedding_net.convnet[0].register_forward_hook(get_activation('fc')) handle.remove() model.embedding_net.convnet[0]._forward_hooks.clear() train_loader.dataset with torch.no_grad(): model.eval() # num_ftrs = model.embedding_net.fc.out_features embeddings = np.zeros((len(train_loader.dataset), num_ftrs)) labels = np.zeros(len(train_loader.dataset)) k = 0 for images, target in train_loader: if cuda: images = images.cuda() embeddings[k:k + len(images)] = model.get_embedding(images).data.cpu().numpy() labels[k:k + len(images)] = target.numpy() k += len(images) features = SaveFeatures(model.embedding_net.convnet[0]) temp = features.features.data.cpu().numpy() del features.features torch.cuda.empty_cache() for images, target in train_loader: if cuda: images = images.cuda() output = model.embedding_net(images) activation = [] def get_activation(): def hook(model, input, output): activation.append(output) print(output) return hook act = activation['conv1'].squeeze() fig, axarr = plt.subplots(act.size(0)) for idx in range(act.size(0)): axarr[idx].imshow(act[idx]) actmap = [] def printnorm(self, input, output): # input is a tuple of packed inputs # output is a Tensor. output.data is the Tensor we are interested print('Inside ' + self.__class__.__name__ + ' forward') print('') print('input: ', type(input)) print('input[0]: ', type(input[0])) print('output: ', type(output)) print('') print('input size:', input[0].size()) print('output size:', output.data.size()) print('output norm:', output.data.norm()) return output.data model.embedding_net.convnet[0].register_forward_hook(printnorm) out = model(input) fig, axarr = plt.subplots(10) for idx in range(10): axarr[idx].imshow(temp[idx,1,:,:])
log_interval = 100 patience = 4 fit_triplet(triplet_train_loader, triplet_test_loader, model, loss_fn, optimizer, scheduler, patience, n_epoch, cuda, log_interval) torch.save(model, "./data/TripletNetwork.pt") model = torch.load("./data/TripletNetwork.pt") #below for unlabeled data TEST_CSV = "../../Data/data/originalVectors" with open(TEST_CSV) as csv_file: with open('../../Data/data/reducedVectors-tripletNet', 'w') as writeFile: csv_reader = csv.reader(csv_file, delimiter=' ') for row in csv_reader: vec = [float(k) for k in row] vec = torch.FloatTensor(vec) writeFile.write( ' '.join(map(str, model.get_embedding(vec).data.numpy())) + "\n") writeFile.close() #below for labeled data # TEST_CSV = "../../Data/data/originalVectors-MNIST" # with open(TEST_CSV) as csv_file: # with open('../../Data/data/reducedVectors-tripletNet-MNIST', 'w') as writeFile: # csv_reader = csv.reader(csv_file, delimiter=' ') # for row in csv_reader: # vec = [float(k) for k in row] # vec = torch.FloatTensor(vec) # writeFile.write(' '.join(map(str, model.get_embedding(vec).data.numpy())) + "\n") # writeFile.close()