代码拉取完成,页面将自动刷新
同步操作将从 yql/pytorch_classification 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
# -*- coding:utf-8 -*-
# @time :2019.03.15
# @IDE : pycharm
# @author :lxztju
# @github : https://github.com/lxztju
import torch
import os
from PIL import Image
import pandas as pd
from tqdm import tqdm
import numpy as np
from collections import Counter
import cfg
from data import tta_test_transform, get_test_transform
def load_checkpoint(filepath):
checkpoint = torch.load(filepath)
model = checkpoint['model'] # 提取网络结构
model.load_state_dict(checkpoint['model_state_dict']) # 加载网络权重参数
for parameter in model.parameters():
parameter.requires_grad = False
model.eval()
return model
def predict(model):
# 读入模型
model = load_checkpoint(model)
print('..... Finished loading model! ......')
##将模型放置在gpu上运行
if torch.cuda.is_available():
model.cuda()
pred_list, _id = [], []
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)
prediction = torch.argmax(out, dim=1).cpu().item()
pred_list.append(prediction)
return _id, pred_list
def tta_predict(model):
# 读入模型
model = load_checkpoint(model)
print('..... Finished loading model! ......')
##将模型放置在gpu上运行
if torch.cuda.is_available():
model.cuda()
pred_list, _id = [], []
for i in tqdm(range(len(imgs))):
img_path = imgs[i].strip()
# print(img_path)
_id.append(int(os.path.basename(img_path).split('.')[0]))
img1 = Image.open(img_path).convert('RGB')
# print(type(img))
pred = []
for i in range(8):
img = tta_test_transform(size=cfg.INPUT_SIZE)(img1).unsqueeze(0)
if torch.cuda.is_available():
img = img.cuda()
with torch.no_grad():
out = model(img)
prediction = torch.argmax(out, dim=1).cpu().item()
pred.append(prediction)
res = Counter(pred).most_common(1)[0][0]
pred_list.append(res)
return _id, pred_list
if __name__ == "__main__":
trained_model = cfg.TRAINED_MODEL
model_name = cfg.model_name
with open(cfg.TEST_LABEL_DIR, 'r')as f:
imgs = f.readlines()
# _id, pred_list = tta_predict(trained_model)
_id, pred_list = predict(trained_model)
submission = pd.DataFrame({"ID": _id, "Label": pred_list})
submission.to_csv(cfg.BASE + '{}_submission.csv'
.format(model_name), index=False, header=False)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。