Diff
checker
文本
文本
圖像
文檔
Excel
文件夾
Legal
Enterprise
桌面版
定價
登入
下載 Diffchecker 桌面版
比較文本
尋找兩個文字檔案之間的差異
工具
歷史
即時編輯器
摺疊未變更行
關閉換行
檢視
拆分
統一
比對精度
智能
單詞
字符
語法突出顯示
選擇語法
忽略
文字轉換
前往第一個差異
編輯輸入
Diffchecker Desktop
執行Diffchecker最安全的方式。取得Diffchecker桌面應用程式:您的差異永遠不會離開您的電腦!
取得桌面版
Untitled diff
建立於
8 年前
差異永不過期
清除
匯出
分享
解釋
3 刪除
行
總計
刪除
字符
總計
刪除
要繼續使用此功能,請升級到
Diff
checker
Pro
查看價格
62 行
全部複製
10 新增
行
總計
新增
字符
總計
新增
要繼續使用此功能,請升級到
Diff
checker
Pro
查看價格
66 行
全部複製
複製
已複製
複製
已複製
#Modified from https://github.com/amrit-das/Custom-Model-Training-PyTorch/blob/master/predict.py
import torch
import torch
import torch.nn as nn
import torch.nn as nn
複製
已複製
複製
已複製
from torchvision.models import resnet18
#
from torchvision.models import resnet18
from torchvision.transforms import transforms
from torchvision.transforms import transforms
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
import numpy as np
import numpy as np
from torch.autograd import Variable
from torch.autograd import Variable
import torch.functional as F
import torch.functional as F
from PIL import Image
from PIL import Image
import os
import os
import sys
import sys
import argparse
import argparse
複製
已複製
複製
已複製
from prune import *
from finetune import *
parser = argparse.ArgumentParser(description = 'To Predict from a trained model')
parser = argparse.ArgumentParser(description = 'To Predict from a trained model')
parser.add_argument('-i','--image', dest = 'image_name', required = True, help='Path to the image file')
parser.add_argument('-i','--image', dest = 'image_name', required = True, help='Path to the image file')
parser.add_argument('-m','--model', dest = 'model_name', required = True, help='Path to the model')
parser.add_argument('-m','--model', dest = 'model_name', required = True, help='Path to the model')
parser.add_argument('-n','--num_class',dest = 'num_classes', required = True, help='Number of training classes')
parser.add_argument('-n','--num_class',dest = 'num_classes', required = True, help='Number of training classes')
args = parser.parse_args()
args = parser.parse_args()
複製
已複製
複製
已複製
path_to_model = "./
models/
"+args.model_name
path_to_model = "./
"+args.model_name
checkpoint = torch.load(path_to_model)
#
checkpoint = torch.load(path_to_model)
複製
已複製
複製
已複製
model = resnet18(num_classes = int(args.num_classes))
model = torch.load(args.model_name).cuda()
model.load_state_dict(checkpoint)
#
model = resnet18(num_classes = int(args.num_classes))
#
model.load_state_dict(checkpoint)
model.eval()
model.eval()
def predict_image(image_path):
def predict_image(image_path):
複製
已複製
複製
已複製
print("predic
it
on in progress")
print("predic
ti
on in progress")
image = Image.open(image_path)
image = Image.open(image_path)
transformation = transforms.Compose([
transformation = transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
])
image_tensor = transformation(image).float()
image_tensor = transformation(image).float()
image_tensor = image_tensor.unsqueeze_(0)
image_tensor = image_tensor.unsqueeze_(0)
if torch.cuda.is_available():
if torch.cuda.is_available():
image_tensor.cuda()
image_tensor.cuda()
input = Variable(image_tensor)
input = Variable(image_tensor)
output = model(input)
output = model(input)
index = output.data.numpy().argmax()
index = output.data.numpy().argmax()
return index
return index
def class_mapping(index):
def class_mapping(index):
mapping=open('class_mapping.txt','r')
mapping=open('class_mapping.txt','r')
class_map={}
class_map={}
for line in mapping:
for line in mapping:
l=line.strip('\n').split('~')
l=line.strip('\n').split('~')
class_map[l[1]]=l[0]
class_map[l[1]]=l[0]
return class_map[str(index)]
return class_map[str(index)]
if __name__ == "__main__":
if __name__ == "__main__":
複製
已複製
複製
已複製
imagepath = "./
Predict_Image
/"+args.image_name
imagepath = "./
test/Lemon
/"+args.image_name
prediction = predict_image(imagepath)
prediction = predict_image(imagepath)
name = class_mapping(prediction)
name = class_mapping(prediction)
print("Predicted Class: ",name)
print("Predicted Class: ",name)
已保存差異
原始文本
開啟檔案
import torch import torch.nn as nn from torchvision.models import resnet18 from torchvision.transforms import transforms import matplotlib.pyplot as plt import numpy as np from torch.autograd import Variable import torch.functional as F from PIL import Image import os import sys import argparse parser = argparse.ArgumentParser(description = 'To Predict from a trained model') parser.add_argument('-i','--image', dest = 'image_name', required = True, help='Path to the image file') parser.add_argument('-m','--model', dest = 'model_name', required = True, help='Path to the model') parser.add_argument('-n','--num_class',dest = 'num_classes', required = True, help='Number of training classes') args = parser.parse_args() path_to_model = "./models/"+args.model_name checkpoint = torch.load(path_to_model) model = resnet18(num_classes = int(args.num_classes)) model.load_state_dict(checkpoint) model.eval() def predict_image(image_path): print("prediciton in progress") image = Image.open(image_path) transformation = transforms.Compose([ transforms.RandomResizedCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) image_tensor = transformation(image).float() image_tensor = image_tensor.unsqueeze_(0) if torch.cuda.is_available(): image_tensor.cuda() input = Variable(image_tensor) output = model(input) index = output.data.numpy().argmax() return index def class_mapping(index): mapping=open('class_mapping.txt','r') class_map={} for line in mapping: l=line.strip('\n').split('~') class_map[l[1]]=l[0] return class_map[str(index)] if __name__ == "__main__": imagepath = "./Predict_Image/"+args.image_name prediction = predict_image(imagepath) name = class_mapping(prediction) print("Predicted Class: ",name)
更改後文本
開啟檔案
#Modified from https://github.com/amrit-das/Custom-Model-Training-PyTorch/blob/master/predict.py import torch import torch.nn as nn #from torchvision.models import resnet18 from torchvision.transforms import transforms import matplotlib.pyplot as plt import numpy as np from torch.autograd import Variable import torch.functional as F from PIL import Image import os import sys import argparse from prune import * from finetune import * parser = argparse.ArgumentParser(description = 'To Predict from a trained model') parser.add_argument('-i','--image', dest = 'image_name', required = True, help='Path to the image file') parser.add_argument('-m','--model', dest = 'model_name', required = True, help='Path to the model') parser.add_argument('-n','--num_class',dest = 'num_classes', required = True, help='Number of training classes') args = parser.parse_args() path_to_model = "./"+args.model_name #checkpoint = torch.load(path_to_model) model = torch.load(args.model_name).cuda() #model = resnet18(num_classes = int(args.num_classes)) #model.load_state_dict(checkpoint) model.eval() def predict_image(image_path): print("prediction in progress") image = Image.open(image_path) transformation = transforms.Compose([ transforms.RandomResizedCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) image_tensor = transformation(image).float() image_tensor = image_tensor.unsqueeze_(0) if torch.cuda.is_available(): image_tensor.cuda() input = Variable(image_tensor) output = model(input) index = output.data.numpy().argmax() return index def class_mapping(index): mapping=open('class_mapping.txt','r') class_map={} for line in mapping: l=line.strip('\n').split('~') class_map[l[1]]=l[0] return class_map[str(index)] if __name__ == "__main__": imagepath = "./test/Lemon/"+args.image_name prediction = predict_image(imagepath) name = class_mapping(prediction) print("Predicted Class: ",name)
尋找差異