1 Star 0 Fork 0

崔静静/Image-Classification-with-PyTorch

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
predict.py 1.63 KB
一键复制 编辑 原始数据 按行查看 历史
Rohit Swami 提交于 7年前 . Initial Commit
#!/usr/bin/env python
# coding: utf-8
# Imports here
import matplotlib.pyplot as plt
import torch
import numpy as np
from torch import nn
from torch import optim
import torch.nn.functional as F
from torchvision import datasets, transforms, models
from workspace_utils import active_session
from PIL import Image
import argparse
import json
import setup
parser = argparse.ArgumentParser()
parser.add_argument('--img_path', action='store', dest='img_path', help='path of image to predict', required=True)
parser.add_argument('--cp_dir', action='store', dest='cp_path', help='path of checkpoint', required=True)
parser.add_argument('--top_k', action="store", default=5, dest="top_k", type=int)
parser.add_argument('--category_names', action="store", dest="category_names", default='cat_to_name.json')
parser.add_argument('--gpu', action='store_true', default=False, dest='boolean_t', help='Set a switch to use GPU')
results = parser.parse_args()
img_path = results.img_path
checkpoint_path = results.cp_path
top_k = results.top_k
category_names = results.category_names
gpu = results.boolean_t
if gpu==True:
using_gpu = torch.cuda.is_available()
device = 'gpu'
print('GPU On');
else:
print('GPU Off');
device = 'cpu'
model = setup.loading_checkpoint(checkpoint_path)
processed_image = setup.process_image(img_path)
probs, classes = setup.predict(processed_image, model, top_k, device)
# Label mapping
cat_to_name = setup.labeling(category_names)
labels = []
for class_index in classes:
labels.append(cat_to_name[str(class_index)])
# Converting from tensor to numpy-array
print('Name of class: ', labels[0])
print('Probability: ', probs)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/cui_jingjing/Image-Classification-with-PyTorch.git
git@gitee.com:cui_jingjing/Image-Classification-with-PyTorch.git
cui_jingjing
Image-Classification-with-PyTorch
Image-Classification-with-PyTorch
master

搜索帮助