def onlytransfer(n_clusters, fb_kmeans=True): X = np.load('data/chan/8chan_pol/VGG16/fc1/featuresx.npy') X = X.astype('float32') pathfile = open('data/chan/8chan_pol/VGG16/fc1/paths.txt', "r") pathlist = pathfile.readlines() pathlist = [path[:-1] for path in pathlist] pathfile.close() if fb_kmeans: #features = torch.from_numpy(features) images_lists, loss = kmeans(X, nmb_clusters=n_clusters, preprocess=False) Y_pred = arrange_clustering(images_lists) else: km = KMeans(n_clusters=n_clusters, n_init=20) Y_pred = km.fit_predict(X) for y_pred, path in zip(Y_pred, pathlist): savedir = '/home/elahe/NortfaceProject/codes/DEC-keras/results/clusters/8chan_pol/%s/%s/%s' % ( 'transfer', 'fc1', y_pred) if not os.path.exists(savedir): os.makedirs(savedir) shutil.copy(path, savedir)
def main(): global args args = parser.parse_args() # fix random seeds torch.manual_seed(args.seed) torch.cuda.manual_seed_all(args.seed) np.random.seed(args.seed) # CNN if args.verbose: print('Architecture: {}'.format(args.arch)) model = models.__dict__[args.arch](sobel=args.sobel) fd = int(model.top_layer.weight.size()[1]) model.top_layer = None model.features = torch.nn.DataParallel(model.features) model.cuda() cudnn.benchmark = True # create optimizer optimizer = torch.optim.SGD( filter(lambda x: x.requires_grad, model.parameters()), lr=args.lr, momentum=args.momentum, weight_decay=10**args.wd, ) # define loss function criterion = nn.CrossEntropyLoss().cuda() # optionally resume from a checkpoint if args.resume: if os.path.isfile(args.resume): print("=> loading checkpoint '{}'".format(args.resume)) checkpoint = torch.load(args.resume) args.start_epoch = checkpoint['epoch'] # remove top_layer parameters from checkpoint for key in checkpoint['state_dict']: if 'top_layer' in key: del checkpoint['state_dict'][key] model.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) print("=> loaded checkpoint '{}' (epoch {})" .format(args.resume, checkpoint['epoch'])) else: print("=> no checkpoint found at '{}'".format(args.resume)) # creating checkpoint repo exp_check = os.path.join(args.exp, 'checkpoints') if not os.path.isdir(exp_check): os.makedirs(exp_check) # creating cluster assignments log cluster_log = Logger(os.path.join(args.exp, 'clusters')) # preprocessing of data normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) tra = [transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), normalize] # load the data end = time.time() dataset = datasets.ImageFolder(args.data, transform=transforms.Compose(tra)) if args.verbose: print('Load dataset: {0:.2f} s'.format(time.time() - end)) dataloader = torch.utils.data.DataLoader(dataset, batch_size=args.batch, num_workers=args.workers, pin_memory=True) # clustering algorithm to use deepcluster = clustering.__dict__[args.clustering](args.nmb_cluster) # training convnet with DeepCluster for epoch in range(args.start_epoch, args.epochs): end = time.time() # remove head model.top_layer = None model.classifier = nn.Sequential(*list(model.classifier.children())[:-1]) # get the features for the whole dataset features = compute_features(dataloader, model, len(dataset)) # cluster the features clustering_loss = deepcluster.cluster(features, verbose=args.verbose) # assign pseudo-labels train_dataset = clustering.cluster_assign(deepcluster.images_lists, dataset.imgs) # uniformely sample per target sampler = UnifLabelSampler(int(args.reassign * len(train_dataset)), deepcluster.images_lists) train_dataloader = torch.utils.data.DataLoader( train_dataset, batch_size=args.batch, num_workers=args.workers, sampler=sampler, pin_memory=True, ) # set last fully connected layer mlp = list(model.classifier.children()) mlp.append(nn.ReLU(inplace=True).cuda()) model.classifier = nn.Sequential(*mlp) model.top_layer = nn.Linear(fd, len(deepcluster.images_lists)) model.top_layer.weight.data.normal_(0, 0.01) model.top_layer.bias.data.zero_() model.top_layer.cuda() # train network with clusters as pseudo-labels end = time.time() loss = train(train_dataloader, model, criterion, optimizer, epoch) # print log if args.verbose: print('###### Epoch [{0}] ###### \n' 'Time: {1:.3f} s\n' 'Clustering loss: {2:.3f} \n' 'ConvNet loss: {3:.3f}' .format(epoch, time.time() - end, clustering_loss, loss)) try: nmi = normalized_mutual_info_score( clustering.arrange_clustering(deepcluster.images_lists), clustering.arrange_clustering(cluster_log.data[-1]) ) print('NMI against previous assignment: {0:.3f}'.format(nmi)) except IndexError: pass print('####################### \n') # save running checkpoint torch.save({'epoch': epoch + 1, 'arch': args.arch, 'state_dict': model.state_dict(), 'optimizer' : optimizer.state_dict()}, os.path.join(args.exp, 'checkpoint.pth.tar')) # save cluster assignments cluster_log.log(deepcluster.images_lists)
def main(args): # fix random seeds seed(31) # CNN model = MobileNetV1(num_classes=100, sobel=True) fd = int(model.top_layer.weight.size()[1]) model.top_layer = None model.features = torch.nn.DataParallel(model.features) model.cuda() cudnn.benchmark = True # create optimizer optimizer = torch.optim.SGD( [x for x in model.parameters() if x.requires_grad], lr=args.lr, momentum=args.momentum, weight_decay=10**args.wd, ) # define loss function criterion = nn.CrossEntropyLoss().cuda() # creating cluster assignments log cluster_log = Logger(os.path.join('./image_list_log/', 'clusters')) end = time.time() # load the data dataset = datasets.ImageFolder(root=r'./dataset/train', transform=transform()) dataloader = torch.utils.data.DataLoader(dataset, batch_size=args.batch, num_workers=args.workers, pin_memory=True) # clustering algorithm to use deepcluster = clustering.__dict__[args.clustering](args.nmb_cluster) print('start train') # training convnet with DeepCluster for epoch in range(args.start_epoch, args.epochs): print(epoch) # remove head model.top_layer = None model.classifier = nn.Sequential( *list(model.classifier.children())[:-1]) # get the features for the whole dataset features = compute_features(dataloader, model, len(dataset), args.batch) # cluster the feature clustering_loss = deepcluster.cluster(features) # assign pseudo-labels train_dataset = clustering.cluster_assign(deepcluster.images_lists, dataset.imgs) # uniformly sample per target sampler = UnifLabelSampler(int(args.reassign * len(train_dataset)), deepcluster.images_lists) train_dataloader = torch.utils.data.DataLoader( train_dataset, batch_size=args.batch, num_workers=args.workers, sampler=sampler, pin_memory=True, ) # set last fully connected layer mlp = list(model.classifier.children()) mlp.append(nn.ReLU(inplace=True).cuda()) model.classifier = nn.Sequential(*mlp) model.top_layer = nn.Linear(fd, len(deepcluster.images_lists)) model.top_layer.weight.data.normal_(0, 0.01) model.top_layer.bias.data.zero_() model.top_layer.cuda() # train network with clusters as pseudo-labels end = time.time() loss = train(train_dataloader, model, criterion, optimizer, epoch, args.lr, args.wd) # print log # print('###### Epoch [{0}] ###### \n' # 'Time: {1:.3f} s\n' # 'Clustering loss: {2:.3f} \n' # 'ConvNet loss: {3:.3f}' # .format(epoch, time.time() - end, clustering_loss, loss)) try: nmi = normalized_mutual_info_score( clustering.arrange_clustering(deepcluster.images_lists), clustering.arrange_clustering(cluster_log.data[-1])) print('NMI against previous assignment: {0:.3f}'.format(nmi)) f = open('result.txt', "a") f.write('NMI against previous assignment: {0:.3f}'.format(nmi)) f.close() # print(loss) except IndexError: pass print('####################### \n') # save cluster assignments cluster_log.log(deepcluster.images_lists)
model.top_layer_class.weight.data.normal_(0, 0.01) model.top_layer_class.bias.data.zero_() model.top_layer_class.cuda() # Joint training for emotional clusters end = time.time() print('======== Epoch ' + str(epoch) + ' ========') loss_joint = train_joint(emo_cluster_dataloader, model, criterion_class, cc_coef, optimizer_class, optimizer_attri, epoch) Loss_Joint.append(loss_joint) print('Loss Joint: ' + str(loss_joint)) try: print('=====================================') nmi = normalized_mutual_info_score( clustering.arrange_clustering(deepcluster.images_lists), clustering.arrange_clustering(cluster_log.data[-1])) NMI.append(nmi) print('NMI against previous assignment: {0:.3f}'.format(nmi)) except IndexError: pass # Validation Stage: considering CCC performance only loss_valid = validation(valid_loader, model, cc_coef) Loss_CCC_Valid.append(loss_valid) cluster_log.log(deepcluster.images_lists) print('Loss Validation CCC: ' + str(loss_valid)) # save model checkpoint based on best validation CCC performance if epoch == 0: # initial CCC value