-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f89abd6
Showing
28 changed files
with
6,297 additions
and
0 deletions.
There are no files selected for viewing
2,319 changes: 2,319 additions & 0 deletions
2,319
resnet18-imagenet-caffe/Resnet50/ResNet-50-deploy.prototxt
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
|
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.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
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.
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 not shown.
Oops, something went wrong.