Skip to content

Commit

Permalink
append loadables resnet
Browse files Browse the repository at this point in the history
  • Loading branch information
LeiWang1999 committed Apr 20, 2021
0 parents commit f89abd6
Show file tree
Hide file tree
Showing 28 changed files with 6,297 additions and 0 deletions.
2,319 changes: 2,319 additions & 0 deletions resnet18-imagenet-caffe/Resnet50/ResNet-50-deploy.prototxt

Large diffs are not rendered by default.

Binary file not shown.
1,090 changes: 1,090 additions & 0 deletions resnet18-imagenet-caffe/deploy.prototxt

Large diffs are not rendered by default.

Binary file added resnet18-imagenet-caffe/loadables/basic.nvdla
Binary file not shown.
Binary file added resnet18-imagenet-caffe/loadables/default.nvdla
Binary file not shown.
Binary file added resnet18-imagenet-caffe/loadables/fast-math.nvdla
Binary file not shown.
Binary file not shown.
37 changes: 37 additions & 0 deletions resnet18-imagenet-caffe/predict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import caffe
import numpy as np
from PIL import Image
def inference(prototxt, caffemodel, image):
net = caffe.Net(prototxt,
caffemodel,
caffe.TEST)
mean = np.ones([3, 224, 224], dtype=np.float)
mean[0,:,:] = 104
mean[1,:,:] = 117
mean[2,:,:] = 123
# create transformer for the input called 'data'
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1)) # move image channels to outermost dimension
transformer.set_mean('data', mean) # subtract the dataset-mean value in each channel
transformer.set_raw_scale('data', 255) # rescale from [0, 1] to [0, 255]
transformer.set_input_scale('data', 1.0)
# transformer.set_channel_swap('data', (2,1,0)) # swap channels from RGB to BGR
image = caffe.io.load_image(image)
print(image.shape)
transformed_image = transformer.preprocess('data', image)
out = net.forward_all(data=np.asarray([transformed_image]))
return out

prototxt_path = 'deploy.prototxt'
caffemodel_path = 'resnet-18.caffemodel'
img_path = 'raw/aircraft_carrier.jpg'
labels_file = 'synset_words.txt'
prediction = inference(prototxt_path, caffemodel_path, img_path)
labels = np.loadtxt(labels_file, str, delimiter='\t')
output_prob = prediction['prob'][0]
# sort top five predictions from softmax output
top_inds = output_prob.argsort()[::-1][:5] # reverse sort and take five largest items
print('probabilities and labels:')
for i in top_inds:
print(i,output_prob[i], labels[i])

27 changes: 27 additions & 0 deletions resnet18-imagenet-caffe/predict_tranformed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import caffe
import numpy as np
import cv2 as cv
def inference(prototxt, caffemodel, image):
net = caffe.Net(prototxt,
caffemodel,
caffe.TEST)
image = cv.imread(image)
print(image.shape)
image = image.transpose((2,0,1))
print(image)
out = net.forward_all(data=np.asarray([image]))
return out

prototxt_path = 'deploy.prototxt'
caffemodel_path = 'resnet-18.caffemodel'
img_path = '0_tench.jpg'
labels_file = 'synset_words.txt'
prediction = inference(prototxt_path, caffemodel_path, 'transformed/' + img_path)
labels = np.loadtxt(labels_file, str, delimiter='\t')
output_prob = prediction['prob'][0]
# sort top five predictions from softmax output
top_inds = output_prob.argsort()[::-1][:5] # reverse sort and take five largest items
print('probabilities and labels:')
for i in top_inds:
print(i,output_prob[i], labels[i])

Binary file added resnet18-imagenet-caffe/raw/aircraft_carrier.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resnet18-imagenet-caffe/raw/hare.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resnet18-imagenet-caffe/raw/printer.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resnet18-imagenet-caffe/raw/tench.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions resnet18-imagenet-caffe/resize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import caffe
import numpy as np
from PIL import Image
import cv2 as cv
def inference(prototxt, caffemodel, image):
net = caffe.Net(prototxt,
caffemodel,
caffe.TEST)
mean = np.ones([3, 224, 224], dtype=np.float)
mean[0,:,:] = 104
mean[1,:,:] = 117
mean[2,:,:] = 123
# create transformer for the input called 'data'
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1)) # move image channels to outermost dimension
# transformer.set_mean('data', mean) # subtract the dataset-mean value in each channel
transformer.set_raw_scale('data', 255) # rescale from [0, 1] to [0, 255]
transformer.set_channel_swap('data', (2,1,0)) # swap channels from RGB to BGR
image = caffe.io.load_image(image)
transformed_image = transformer.preprocess('data', image)
print(transformed_image)
out = net.forward_all(data=np.asarray([transformed_image]))
return transformed_image,out

prototxt_path = 'deploy.prototxt'
caffemodel_path = 'resnet-18.caffemodel'
img_path = 'aircraft_carrier.jpg'
labels_file = 'synset_words.txt'
transformed_image, prediction = inference(prototxt_path, caffemodel_path, 'raw/'+img_path)
labels = np.loadtxt(labels_file, str, delimiter='\t')
output_prob = prediction['prob'][0]
# sort top five predictions from softmax output
top_inds = output_prob.argsort()[::-1][:5] # reverse sort and take five largest items
print('probabilities and labels:')
for i in top_inds:
print(i,output_prob[i], labels[i])

dirname = 'resized/'
filepath = dirname + str(int(top_inds[0])) + '_' + img_path
print(filepath)
saveim = transformed_image.transpose((1, 2, 0))
cv.imwrite(filepath, saveim)
# saveim = Image.fromarray(saveim.astype(np.uint8))
# print('-----------------')
# print(np.array(saveim))
# saveim.save(filepath)
# loadim = Image.open(filepath)
# print('+++++++++++++++')
# print(np.array(loadim))
Binary file added resnet18-imagenet-caffe/resized/0_tench.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resnet18-imagenet-caffe/resized/331_hare.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resnet18-imagenet-caffe/resized/742_printer.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resnet18-imagenet-caffe/resnet-18.caffemodel
Binary file not shown.
Loading

0 comments on commit f89abd6

Please sign in to comment.