def main(): # prepare input data trf = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) inputs = trf(Image.open('./data/dog.jpg')).unsqueeze(0) # load existing model from pytorchcv lib (pretrain=False) net = ptcv_get_model('mobilenetv2_w1', pretrained=False) net.eval() output = net(inputs) # print("original model:",net) # fuse the Batch Normalization net1 = fuse_bn_recursively(copy.deepcopy(net)) net1.eval() output1 = net1(inputs) # print("BN fusion model:",net1) # compare the output print("output of original model:",output.size()) print("=> min : %.4f, mean : %.4f max : %.4f"%(output.min().detach().numpy(),output.mean().detach().numpy(),output.max().detach().numpy())) print("output of BNfusion model:",output1.size()) print("=> min : %.4f, mean : %.4f max : %.4f"%(output1.min().detach().numpy(),output1.mean().detach().numpy(),output1.max().detach().numpy())) # transform to ONNX format for visualization dummy_input = Variable(torch.randn([1, 3, 224, 224])) torch.onnx.export(net, dummy_input, "./data/mobilenet_v2.onnx") torch.onnx.export(net1, dummy_input, "./data/mobilenet_v2_nobn.onnx")
def infer(test_image_data_path, test_meta_data_path): # DONOTCHANGE This Line test_meta_data = pd.read_csv(test_meta_data_path, delimiter=',', header=0) input_size=224 # you can change this according to your model. batch_size=_BATCH_SIZE # you can change this. But when you use 'nsml submit --test' for test infer, there are only 200 number of data. # test time gpu memory error, so i reduce batch size when test time device = 0 dataloader = DataLoader( AIRushDataset(test_image_data_path, test_meta_data, label=None, transform=transforms.Compose([ObjROI() ,transforms.Resize((input_size, input_size)) , transforms.ToTensor() #,transforms.Normalize(mean=mean_v, std=std_v) ])), batch_size=batch_size, shuffle=False, num_workers=3, pin_memory=True) model_nsml.to(device) model_nsml = fuse_bn_recursively(model_nsml) model_nsml.eval() predict_list = [] for batch_idx, image in enumerate(dataloader): image = image.to(device) output = model_nsml(image).double() output_prob = F.softmax(output, dim=1) predict = np.argmax(to_np(output_prob), axis=1) predict_list.append(predict) predict_vector = np.concatenate(predict_list, axis=0) return predict_vector # this return type should be a numpy array which has shape of (138343, 1)
net = convert_resnet_family(net, se) # Benchmarking # First, we run the network the way it is net.eval() with torch.no_grad(): F.softmax(net(img), 1) # Measuring non-optimized model performance times = [] for i in range(50): start = time.time() with torch.no_grad(): res_0 = F.softmax(net(img), 1) times.append(time.time() - start) print('Non fused takes', np.mean(times), 'seconds') net = fuse_bn_recursively(net) net.eval() times = [] for i in range(50): start = time.time() with torch.no_grad(): res_1 = F.softmax(net(img), 1) times.append(time.time() - start) print('Fused takes', np.mean(times), 'seconds') diff = res_0 - res_1 print('L2 Norm of the element-wise difference:', diff.norm().item())
modelA = EfficientNet.from_name('efficientnet-b4', override_params={'num_classes': args.output_size}) modelB = make_model('se_resnext50_32x4d', num_classes=args.output_size, pretrained=False, pool=nn.AdaptiveAvgPool2d(1)) modelC = make_model('nasnetamobile', num_classes=args.output_size, pretrained=False, pool=nn.AdaptiveAvgPool2d(1)) # DONOTCHANGE: They are reserved for nsml bind_model(modelA) re_train_info = use_ensemble_model_session[0]#'efficientnet-b4' nsml.load(checkpoint=re_train_info['checkpoint'], session=re_train_info['session']) bind_model(modelB) re_train_info = use_ensemble_model_session[1]#'se_resnext50_32x4d' nsml.load(checkpoint=re_train_info['checkpoint'], session=re_train_info['session']) bind_model(modelC) re_train_info = use_ensemble_model_session[2]#'nasnetamobile' nsml.load(checkpoint=re_train_info['checkpoint'], session=re_train_info['session']) model = MyEnsembleTTA(modelA,modelB,modelC) model = fuse_bn_recursively(model) model = model.to(device) #use gpu #summary(model, (3,args.input_size,args.input_size)) # DONOTCHANGE: They are reserved for nsml bind_model(model) #nsml.load(checkpoint='3', session='team_27/airush1/392 ') #nsml.save('dontgiveup') if args.pause: nsml.paused(scope=locals()) if args.mode == "train": # Warning: Do not load data before this line dataloader, valid_dataloader = train_dataloader(args.input_size, args.batch_size*10, args.num_workers, test_bs = False , br_multi_oh=True#) ,print_nor_info = False,use_last_fine_tune=True)