def predict(model): # 读入模型 model = load_checkpoint(model) print('..... Finished loading model! ......') ##将模型放置在gpu上运行 if torch.cuda.is_available(): model.cuda() pred_list, _id, pred_pro_list = [], [], [] for i in tqdm(range(len(imgs))): img_path = imgs[i].strip() # print(img_path) _id.append(os.path.basename(img_path).split('.')[0]) img = Image.open(img_path).convert('RGB') # print(type(img)) img = get_test_transform(size=cfg.INPUT_SIZE)(img).unsqueeze(0) if torch.cuda.is_available(): img = img.cuda() with torch.no_grad(): out = model(img) softmax = F.softmax(out, dim=-1) #print(softmax.tolist()[0]) pred_pro_list.append(softmax.tolist()[0]) prediction = torch.argmax(out, dim=1).cpu().item() pred_list.append(prediction) return _id, pred_list, pred_pro_list
def __init__(self, label_file, imageset): ''' img_dir: 图片路径:img_dir + img_name.jpg构成图片的完整路径 ''' # 所有图片的绝对路径 with open(label_file, 'r') as f: #label_file的格式, (label_file image_label) self.imgs = list(map(lambda line: line.strip().split(' '), f)) # 相关预处理的初始化 # self.transforms=transform self.img_aug=True if imageset == 'train': self.transform= get_train_transform(size=cfg.INPUT_SIZE) else: self.transform = get_test_transform(size = cfg.INPUT_SIZE) self.input_size = cfg.INPUT_SIZE
def predict(model): # 读入模型 model = load_checkpoint(model) print('..... Finished loading model! ......') ##将模型放置在gpu上运行 if torch.cuda.is_available(): model.cuda() img = Image.open(img_path).convert('RGB') img = get_test_transform(size=cfg.INPUT_SIZE)(img).unsqueeze(0) if torch.cuda.is_available(): img = img.cuda() with torch.no_grad(): x = model.conv1(img) draw_features(8, 8, 64, x.cpu().numpy(), "{}/f1_conv1.png".format(savepath))
def __init__(self, label_file, imageset): ''' img_dir: 图片路径:img_dir + img_name.jpg构成图片的完整路径 ''' # 所有图片的绝对路径 label_file = "D:\\05分类图片\\pytorch_classification-master\\data\\train.txt" print(label_file) with open(label_file, 'r') as f: #label_file的格式, (label_file image_label) self.imgs = list(map(lambda line: line.strip().split(' '), f)) # 相关预处理的初始化 # self.transforms=transform self.img_aug=True if imageset == 'train': self.transform= get_train_transform(size=cfg.INPUT_SIZE) else: self.transform = get_test_transform(size = cfg.INPUT_SIZE) self.eraser = get_random_eraser( s_h=0.1, pixel_level=True) self.input_size = cfg.INPUT_SIZE