Skip to content

Commit 1166e08

Browse files
authored
Add files via upload
1 parent d2ec3ef commit 1166e08

12 files changed

+427
-0
lines changed

CAMheatmap_squeezenet.m

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
function HeatMap = CAMheatmap_squeezenet(image,act,scores,Weights)
2+
3+
coder.gpu.kernelfun
4+
5+
imageActivations = act;
6+
7+
% check the classified category from scores
8+
if scores(1) > scores(2)
9+
classIndex = 1;
10+
else
11+
classIndex = 2;
12+
end
13+
% get classified category's Weight data at the fully conntected layer
14+
weightVector = Weights(classIndex,:);
15+
16+
% calculate Classification Activation Map
17+
weightVectorSize = size(weightVector);
18+
weightVector = reshape(weightVector,[1 weightVectorSize]);
19+
dotProduct = bsxfun(@times,imageActivations,weightVector);
20+
classActivationMap = sum(dotProduct,3);
21+
originalSize = size(image);
22+
% resize
23+
classActivationMap = imresize(classActivationMap,originalSize(1:2));
24+
25+
imgmap = double(classActivationMap);
26+
range = [min(imgmap(:)) max(imgmap(:))];
27+
28+
heatmap_gray = imgmap - range(1);
29+
heatmap_gray = heatmap_gray/(range(2)-range(1));
30+
heatmap_x = round(heatmap_gray * 255);
31+
32+
%persistent tbl;
33+
%if isempty(tbl)
34+
tbl = coder.const(jet(256));
35+
%end
36+
37+
% Make sure A is in the range from 1 to size(cm,1)
38+
a = max(1,min(heatmap_x,size(tbl,1)));
39+
40+
useMap = true;
41+
if ~useMap
42+
b = uint8(a);
43+
bw = imbinarize(b);
44+
sz = size(bw);
45+
flag = (sz(1)*sz(2)/2) > sum(bw(:));
46+
47+
%
48+
if flag
49+
pad = b;
50+
pad(~bw) = 0;
51+
else
52+
pad = 255 - b;
53+
pad(bw) = 0;
54+
end
55+
pad = pad + 1;
56+
else
57+
pad = a;
58+
end
59+
60+
% Extract r,g,b components
61+
r = tbl(pad,1);
62+
r = reshape(r,[227 227]);
63+
g = tbl(pad,2);
64+
g = reshape(g,[227 227]);
65+
b = tbl(pad,3);
66+
b = reshape(b,[227 227]);
67+
68+
tmp = im2double(image)*0.3 + (cat(3, r, g, b) * 0.5);
69+
HeatMap = uint8(tmp*255);
70+
71+
end
72+
73+
%% Copyright 2020 The MathWorks, Inc.
74+
75+
%% Reference
76+
% Learning Deep Features for Discriminative Localization
77+
% Bolei Zhou, Aditya Khosla, Agata Lapedriza, Aude Oliva, Antonio Torralba Computer Science and Arti?cial Intelligence Laboratory, MIT
78+
% http://cnnlocalization.csail.mit.edu/

computeModelAccuracy.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function accuracy = computeModelAccuracy(predictionScores, net, dataStore)
2+
%% Computes model-level accuracy statistics
3+
4+
% Load ground truth
5+
tmp = readall(dataStore);
6+
groundTruth = tmp.response;
7+
8+
% Compare with predicted label with actual ground truth
9+
predictionError = {};
10+
for idx=1:numel(groundTruth)
11+
[~, idy] = max(predictionScores(idx,:));
12+
yActual = net.Layers(end).Classes(idy);
13+
predictionError{end+1} = (yActual == groundTruth(idx)); %#ok
14+
end
15+
16+
% Sum all prediction errors.
17+
predictionError = [predictionError{:}];
18+
accuracy = sum(predictionError)/numel(predictionError);
19+
end
20+
21+
%% Copyright 2020 The MathWorks, Inc.

gpu_predict.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function [out, act] = gpu_predict(in)
2+
%#codegen
3+
4+
persistent mynet;
5+
6+
if isempty(mynet)
7+
% Load the mySqueezenet.mat into the persistent mynet for code
8+
% generation
9+
mynet = coder.loadDeepLearningNetwork('mySqueezeNet.mat','convnet');
10+
end
11+
12+
out = mynet.predict(in);
13+
act = mynet.activations(in, 'relu_conv10','OutputAs','channels');
14+
15+
end
16+
17+
%% Copyright 2020 The MathWorks, Inc.

main_nutsCam.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/* Copyright 2020 The MathWorks, Inc. */
2+
3+
/* Headers */
4+
#include <stdio.h>
5+
#include <cuda.h>
6+
#include <sys/timeb.h>
7+
/* Opencv includes*/
8+
#include "opencv2/opencv.hpp"
9+
10+
using namespace cv;
11+
12+
/* targetFunction source headers*/
13+
#include "targetFunction.h"
14+
15+
int main(int argc, const char * const argv[])
16+
{
17+
18+
unsigned int wi = 320;
19+
unsigned int he = 240;
20+
//unsigned int ch = 3;
21+
//unsigned int size = wi*he*ch;
22+
23+
int sw;
24+
sw = atoi(argv[1]);
25+
26+
/* Input arguments */
27+
VideoCapture cap;
28+
if ( sw < 3 ) {
29+
if ( sw < 2 ) {
30+
//VideoCapture cap(atoi(argv[2])); //use device number for camera.
31+
cap.open(atoi(argv[2])); //use device number for camera.
32+
if (!cap.isOpened()) {
33+
printf("Could not open the video capture device.\n");
34+
return -1;
35+
}
36+
cap.set(CAP_PROP_FRAME_WIDTH, wi);
37+
cap.set(CAP_PROP_FRAME_HEIGHT, he);
38+
//cap.set(CAP_PROP_AUTOFOCUS, 0);
39+
//cap.set(28, 70);
40+
} else {
41+
//VideoCapture cap(argv[2]);
42+
cap.open(argv[2]);
43+
if (!cap.isOpened()) {
44+
printf("Could not open the video capture device.\n");
45+
return -1;
46+
}
47+
}
48+
}
49+
50+
namedWindow("Defect Detection Demo",CV_WINDOW_NORMAL);
51+
resizeWindow("Defect Detection Demo", 300,300);
52+
53+
cudaEvent_t start, stop;
54+
float fps=0;
55+
Mat inImg;
56+
//Mat im;
57+
Mat outImg;
58+
cudaEventCreate(&start);
59+
cudaEventCreate(&stop);
60+
unsigned char output[230400];
61+
62+
/* Start reading frames */
63+
for(;;)
64+
{
65+
//Mat inImg;
66+
67+
cudaEventRecord(start);
68+
69+
if ( sw < 3 ) {
70+
cap >> inImg;
71+
} else {
72+
inImg = imread(argv[2], 1);
73+
}
74+
75+
if (inImg.empty()) {
76+
if ( sw == 2 ) {
77+
cap.set(CAP_PROP_POS_FRAMES, 0);
78+
cap >> inImg;
79+
} else {
80+
printf("Could not open the video capture device.\n");
81+
break;
82+
}
83+
}
84+
85+
//Size size(wi,he);
86+
//resize(inImg,im,size);
87+
88+
targetFunction(inImg.data, output);
89+
cudaEventRecord(stop);
90+
cudaEventSynchronize(stop);
91+
92+
Mat outImg(inImg.size(),inImg.type(),output);
93+
94+
char strbuf[50];
95+
float milliseconds = -1.0;
96+
cudaEventElapsedTime(&milliseconds, start, stop);
97+
fps = fps*.9+1000.0/milliseconds*.1;
98+
99+
if( waitKey(50)%256 == 27 ) break; // stop capturing by pressing ESC
100+
101+
if ( sw == 3 ) {
102+
imwrite("output.png", outImg);
103+
break;
104+
}
105+
106+
sprintf (strbuf, "%.2f FPS", fps);
107+
putText(outImg, strbuf, cvPoint(120,30), CV_FONT_HERSHEY_DUPLEX, 0.5, CV_RGB(0,0,0), 1);
108+
109+
imshow("Defect Detection Demo", outImg);
110+
}
111+
112+
destroyWindow("Defect Detection Demo");
113+
114+
return 0;
115+
}
116+

mat2ocv.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function out = mat2ocv(img)
2+
% Convert image data format to OpenCV compatible format
3+
4+
sz = size(img);
5+
Iocv = zeros([prod(sz), 1], 'uint8');
6+
% permute data
7+
Ir = permute(img, [2 1 3]);
8+
Iocv(1:3:end) = Ir(:,:,3);
9+
Iocv(2:3:end) = Ir(:,:,2);
10+
Iocv(3:3:end) = Ir(:,:,1);
11+
% reshape
12+
out = reshape(Iocv, sz);
13+
end
14+
15+
16+
%% Copyright 2020 The MathWorks, Inc.

myNDNet_Postprocess.m

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function [imgPacked] = myNDNet_Postprocess(Iori, num, bbox, scores, wi, he, ch, HeatMap)
2+
3+
%% Parameters of input image
4+
% wi = 640;
5+
% he = 480;
6+
% ch = 3;
7+
coder.gpu.kernelfun
8+
9+
labeltbl = {'Defective';'Good'};
10+
colortbl = [255 255 102; 73 6 248];
11+
12+
%% Insert annotation as an post-processing
13+
img2 = Iori;
14+
for i = 1:num
15+
idx = (scores(1, i) < scores(2, i)) + 1;
16+
bbox = round(bbox);
17+
img2 = insertObjectAnnotation(img2, 'rectangle', bbox(i,:),...
18+
labeltbl{idx}, 'FontSize', 16, 'Color', colortbl(idx,:));
19+
tmp = imresize(HeatMap(:,:,:,i), [bbox(i,4)+1 bbox(i,3)+1]);
20+
img2(bbox(i,2):bbox(i,2)+bbox(i,4),bbox(i,1):bbox(i,1)+bbox(i,3),:) = tmp;
21+
end
22+
23+
%% Convert image data format to OpenCV compatible image format
24+
imgPacked = coder.nullcopy(zeros([1,he*wi*ch], 'uint8'));
25+
26+
for i = 1:he
27+
for j = 1:wi
28+
imgPacked((i-1)*wi*ch + (j-1)*ch + 3) = img2(i,j,1);
29+
imgPacked((i-1)*wi*ch + (j-1)*ch + 2) = img2(i,j,2);
30+
imgPacked((i-1)*wi*ch + (j-1)*ch + 1) = img2(i,j,3);
31+
end
32+
end
33+
34+
end
35+
36+
%% Copyright 2020 The MathWorks, Inc.

myNDNet_Preprocess.m

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
function [Iori, nuts, num1, bbox] = myNDNet_Preprocess(inImg)
2+
%#codegen
3+
coder.gpu.kernelfun
4+
5+
%% Parameters of input image
6+
sz = size(inImg);
7+
wi = sz(2);
8+
he = sz(1);
9+
ch = sz(3);
10+
11+
ratio = wi*he*ch / 921600;
12+
imgSz = 227;
13+
14+
%% Convert image data format to MATLAB compatible image format
15+
Iori = coder.nullcopy(zeros([he,wi,ch], 'uint8'));
16+
%Iori = inImg;
17+
% r = reshape(inImg(3:3:end), [he,wi]);
18+
% g = reshape(inImg(2:3:end), [he,wi]);
19+
% b = reshape(inImg(1:3:end), [he,wi]);
20+
% Iori = cat(3, r, g, b);
21+
for i = 1:he
22+
for j = 1:wi
23+
Iori(i,j,3) = inImg((i-1)*wi*ch + (j-1)*ch+1);
24+
Iori(i,j,2) = inImg((i-1)*wi*ch + (j-1)*ch+2);
25+
Iori(i,j,1) = inImg((i-1)*wi*ch + (j-1)*ch+3);
26+
end
27+
end
28+
29+
%% Extract where the nuts are, as an ROI
30+
hsv = rgb2hsv(Iori); %RGB to HSV color space conversion
31+
th = otsuthresh(imhist(hsv(:,:,2))); %Binarize image
32+
bw = hsv(:,:,2) < th;
33+
bw = bwareaopen(bw, 20000*ratio); %Remove noise
34+
35+
%% Measure boundingbox of roi instead of using regionprops
36+
bwl = bwlabel(bw);
37+
num = max(bwl(:));
38+
39+
gray = rgb2gray(Iori);
40+
gray = imadjust(gray);
41+
42+
bbox = coder.nullcopy(zeros(4));
43+
nuts = coder.nullcopy(zeros([imgSz,imgSz,4], 'uint8'));
44+
k=0;
45+
if num > 0 && num < 5
46+
for i = 1:num
47+
[r, c] = find(bwl == i);
48+
rmin = min(r);
49+
rmax = max(r);
50+
cmin = min(c);
51+
cmax = max(c);
52+
%Crop the image if measured area is in a specified range
53+
if (cmax-cmin) * (rmax-rmin) < 20000 && rmin >0 && cmin >0
54+
k=k+1;
55+
bbox(k, :) = [cmin, rmin, cmax-cmin, rmax-rmin];
56+
tmp = imcrop(gray, bbox(k,:));
57+
nuts(:,:,k) = imresize(tmp, [imgSz imgSz]);
58+
end
59+
end
60+
num1 = k;
61+
else
62+
num1 = 0;
63+
end
64+
65+
end
66+
67+
68+
%% Copyright 2020 The MathWorks, Inc.

ocv2mat.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function out = ocv2mat(img, sz)
2+
% Convert image data format to MATLAB compatible image format
3+
4+
sec = sz(1)*sz(2);
5+
Im = zeros([sz(2), sz(1), sz(3)], 'uint8');
6+
Im(1:sec) = img(3:3:end);
7+
Im(sec+1:sec*2) = img(2:3:end);
8+
Im(sec*2+1:sec*3) = img(1:3:end);
9+
out = permute(Im, [2 1 3]);
10+
end
11+
12+
13+
%% Copyright 2020 The MathWorks, Inc.

0 commit comments

Comments
 (0)