std = [0.229] device = torch.device('cuda') model_path = 'checkpoints/019.pt' test_set = TinyMind(mode='test', root='data/', fpath="test2.txt", trans=T.Compose([Norm(mean, std), to_tensor()])) test_loader = DataLoader(dataset=test_set, batch_size=256, shuffle=False, num_workers=0) net = resnet50().to(device) state = torch.load(model_path) net.load_state_dict(state['state_dict']) classes = [ '且', '世', '东', '九', '亭', '今', '从', '令', '作', '使', '侯', '元', '光', '利', '印', '去', '受', '右', '司', '合', '名', '周', '命', '和', '唯', '堂', '士', '多', '夜', '奉', '女', '好', '始', '字', '孝', '守', '宗', '官', '定', '宜', '室', '家', '寒', '左', '常', '建', '徐', '御', '必', '思', '意', '我', '敬', '新', '易', '春', '更', '朝', '李', '来', '林', '正', '武', '氏', '永', '流', '海', '深', '清', '游', '父', '物', '玉', '用', '申', '白', '皇', '益', '福', '秋', '立', '章', '老', '臣', '良', '莫', '虎', '衣', '西', '起', '足', '身', '通', '遂', '重', '陵', '雨', '高', '黄', '鼎' ] results_csv = csv.writer( open('./data/result.csv', 'w', encoding='utf-8', newline='')) results_csv.writerow(['filename', 'label'])
def network(args): if args.net == 'alexnet': from models.AlexNet import alexnet net = alexnet(num_class=args.num_class) elif args.net == 'fractalnet34': from models.FratalNet import fractalnet34 net = fractalnet34(num_class=args.num_class, initialize=args.initialize) elif args.net == 'googlenet': from models.GoogLeNet import googlenet net = googlenet(num_class=args.num_class, initialize=args.initialize) elif args.net == 'inception_v3': from models.InceptionV3 import inception_v3 net = inception_v3(num_class=args.num_class, initialize=args.initialize) elif args.net == 'mobilenet_v1': from models.MobileNetV1 import mobilenet_v1 net = mobilenet_v1(num_class=args.num_class, initialize=args.initialize) elif args.net == 'preactresnet18': from models.PreActResNet import peractresnet18 net = peractresnet18(num_class=args.num_class, initialize=args.initialize) elif args.net == 'preactresnet34': from models.PreActResNet import peractresnet34 net = peractresnet34(num_class=args.num_class, initialize=args.initialize) elif args.net == 'preactresnet50': from models.PreActResNet import peractresnet50 net = peractresnet50(num_class=args.num_class, initialize=args.initialize) elif args.net == 'preactresnet101': from models.PreActResNet import peractresnet101 net = peractresnet101(num_class=args.num_class, initialize=args.initialize) elif args.net == 'preactresnet152': from models.PreActResNet import peractresnet152 net = peractresnet152(num_class=args.num_class, initialize=args.initialize) elif args.net == 'resnet18': from models.ResNet import resnet18 net = resnet18(num_class=args.num_class, initialize=args.initialize) elif args.net == 'resnet34': from models.ResNet import resnet34 net = resnet34(num_class=args.num_class, initialize=args.initialize) elif args.net == 'resnet50': from models.ResNet import resnet50 net = resnet50(num_class=args.num_class, initialize=args.initialize) elif args.net == 'resnet101': from models.ResNet import resnet101 net = resnet101(num_class=args.num_class, initialize=args.initialize) elif args.net == 'resnet152': from models.ResNet import resnet152 net = resnet152(num_class=args.num_class, initialize=args.initialize) elif args.net == 'se_resnet50': from models.SEResNet import se_resnet50 net = se_resnet50(num_class=args.num_class, initialize=args.initialize) elif args.net == 'se_resnet101': from models.SEResNet import se_resnet101 net = se_resnet101(num_class=args.num_class, initialize=args.initialize) elif args.net == 'se_resnet152': from models.SEResNet import se_resnet152 net = se_resnet152(num_class=args.num_class, initialize=args.initialize) elif args.net == 'shufflenet_05x_g1': from models.ShuffleNet import shufflenet_05x_g1 net = shufflenet_05x_g1(num_class=args.num_class, initialize=args.initialize) elif args.net == 'shufflenet_05x_g2': from models.ShuffleNet import shufflenet_05x_g2 net = shufflenet_05x_g2(num_class=args.num_class, initialize=args.initialize) elif args.net == 'shufflenet_05x_g3': from models.ShuffleNet import shufflenet_05x_g3 net = shufflenet_05x_g3(num_class=args.num_class, initialize=args.initialize) elif args.net == 'shufflenet_05x_g4': from models.ShuffleNet import shufflenet_05x_g4 net = shufflenet_05x_g4(num_class=args.num_class, initialize=args.initialize) elif args.net == 'shufflenet_025x_g1': from models.ShuffleNet import shufflenet_025x_g1 net = shufflenet_025x_g1(num_class=args.num_class, initialize=args.initialize) elif args.net == 'shufflenet_025x_g2': from models.ShuffleNet import shufflenet_025x_g2 net = shufflenet_025x_g2(num_class=args.num_class, initialize=args.initialize) elif args.net == 'shufflenet_025x_g3': from models.ShuffleNet import shufflenet_025x_g3 net = shufflenet_025x_g3(num_class=args.num_class, initialize=args.initialize) elif args.net == 'shufflenet_025x_g4': from models.ShuffleNet import shufflenet_025x_g4 net = shufflenet_025x_g4(num_class=args.num_class, initialize=args.initialize) elif args.net == 'shufflenet_1x_g1': from models.ShuffleNet import shufflenet_1x_g1 net = shufflenet_1x_g1(num_class=args.num_class, initialize=args.initialize) elif args.net == 'shufflenet_1x_g2': from models.ShuffleNet import shufflenet_1x_g2 net = shufflenet_1x_g2(num_class=args.num_class, initialize=args.initialize) elif args.net == 'shufflenet_1x_g3': from models.ShuffleNet import shufflenet_1x_g3 net = shufflenet_1x_g3(num_class=args.num_class, initialize=args.initialize) elif args.net == 'shufflenet_1x_g4': from models.ShuffleNet import shufflenet_1x_g4 net = shufflenet_1x_g4(num_class=args.num_class, initialize=args.initialize) elif args.net == 'squeezenet': from models.SqueezeNet import squeezenet net = squeezenet(num_class=args.num_class, initialize=args.initialize) elif args.net == 'stochastic_depth18': from models.StochasticDepth import stochastic_depth18 net = stochastic_depth18(num_class=args.num_class, initialize=args.initialize) elif args.net == 'stochastic_depth34': from models.StochasticDepth import stochastic_depth34 net = stochastic_depth34(num_class=args.num_class, initialize=args.initialize) elif args.net == 'stochastic_depth50': from models.StochasticDepth import stochastic_depth50 net = stochastic_depth50(num_class=args.num_class, initialize=args.initialize) elif args.net == 'stochastic_depth101': from models.StochasticDepth import stochastic_depth101 net = stochastic_depth101(num_class=args.num_class, initialize=args.initialize) elif args.net == 'stochastic_depth152': from models.StochasticDepth import stochastic_depth152 net = stochastic_depth152(num_class=args.num_class, initialize=args.initialize) elif args.net == 'vgg_11': from models.VGG import vgg_11 net = vgg_11(num_class=args.num_class, initialize=args.initialize) elif args.net == 'vgg_11_lrn': from models.VGG import vgg_11_lrn net = vgg_11_lrn(num_class=args.num_class, initialize=args.initialize) elif args.net == 'vgg_13': from models.VGG import vgg_13 net = vgg_13(num_class=args.num_class, initialize=args.initialize) elif args.net == 'vgg_16_c': from models.VGG import vgg_16_c net = vgg_16_c(num_class=args.num_class, initialize=args.initialize) elif args.net == 'vgg_16_d': from models.VGG import vgg_16_d net = vgg_16_d(num_class=args.num_class, initialize=args.initialize) elif args.net == 'vgg_19': from models.VGG import vgg_19 net = vgg_19(num_class=args.num_class, initialize=args.initialize) elif args.net == 'wide_resnet40_4': from models.Wide_ResNet import wide_resnet40_4 net = wide_resnet40_4(num_class=args.num_class, initialize=args.initialize) elif args.net == 'wide_resnet16_8': from models.Wide_ResNet import wide_resnet16_8 net = wide_resnet16_8(num_class=args.num_class, initialize=args.initialize) elif args.net == 'wide_resnet28_10': from models.Wide_ResNet import wide_resnet28_10 net = wide_resnet28_10(num_class=args.num_class, initialize=args.initialize) elif args.net == 'resnet110': from models.Wide_ResNet import resnet110 net = resnet110(num_class=args.num_class, initialize=args.initialize) elif args.net == 'xception': from models.Xception import xception net = xception(num_class=args.num_class, initialize=args.initialize) return net
# train teacher model from pretrained resnet teacher_model = torch_models.resnet34(pretrained=True) teacher_model.fc = nn.Linear(512, 10) # freeze earlier layers (first 7 out of 10) ct = 0 for child in teacher_model.children(): ct += 1 if ct < 7: for param in child.parameters(): param.requires_grad = False teacher_model = teacher_model.to(device) teacher_model = nn.DataParallel(teacher_model, device_ids=[0, 1]) # teacher_model.load_state_dict(checkpoint['teacher_model']) student_model = resnet50(in_channels=3, n_classes=10).to(device) student_model = nn.DataParallel(student_model, device_ids=[0, 1]) # student_model.load_state_dict(checkpoint['student_model']) with wandb.init(project="MPL-Cifar10"): train_mpl(teacher_model, student_model, sup_dl, unsup_dl, batch_size=BATCH_SIZE, dataset=DATASET, num_epochs=N_EPOCHS, learning_rate=LR, weight_u=WEIGHT_U, uda_threshold=UDA_THRESHOLD, n_student_steps=N_STUDENT_STEPS,
download=True, transform=transform) val_loader = torch.utils.data.DataLoader(valset, batch_size=10, shuffle=True, num_workers=2) alexnet = AlexNet().cuda() alexnetexpander = alexnetexpander(sparsity=10).cuda() alexnetskip = alexnetskip(sparsity=10).cuda() resnet34 = resnet34().cuda() resnet34expander = resnet34expander(sparsity=5).cuda() resnet34skip = resnet34skip(sparsity=5).cuda() resnet50 = resnet50().cuda() resnet50expander = resnet50expander(sparsity=10).cuda() resnet50skip = resnet50skip(sparsity=10).cuda() vgg16 = vgg16().cuda() vggexpander16 = vggexpander16(sparsity=10).cuda() vggskip16 = vggskip16(sparsity=2).cuda() #model_10 = Netexpander(sparsity=45).cuda() model = Net().cuda() modelx = Netexpander(sparsity=10).cuda() #Wprune = adj_matrix(modelx,10) #W_binary = adj_matrix(model,10) #Wprune,eprune,vprune = EVD(modelx) #raw = low_rank(Wprune,eprune,vprune)