Skip to content

Commit 459d58c

Browse files
authored
add grasp_detectoin part
1 parent dec6f76 commit 459d58c

18 files changed

+883
-0
lines changed

grasp_detection/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# grasp_detection
2+
3+
4+
5+
6+
### Main system requirements:
7+
- CUDA 11.1.1
8+
- Python 3.8
9+
- TensorFlow v2.4.1
10+
11+
12+
13+
### Quick Start
14+
1. Clone this repository
15+
```
16+
git clone https://github.com/ivalab/grasp_multiObject_multiGrasp.git
17+
cd grasp_multiObject_multiGrasp
18+
```
19+
20+
2. Build Cython modules
21+
```
22+
cd lib
23+
make clean
24+
make
25+
cd ..
26+
```
27+
28+
3. Install [Python COCO API](https://github.com/cocodataset/cocoapi)
29+
```
30+
cd data
31+
git clone https://github.com/pdollar/coco.git
32+
cd coco/PythonAPI
33+
make
34+
cd ../../..
35+
```
36+
37+
4. Put models under `output/res50/train/default/`
38+
4. Run demo
39+
40+
```
41+
./tools/demo_graspRGD.py --net res50 --dataset grasp
42+
```
43+
6. Run with own images
44+
45+
If you want to run demo with your own images, just put your image into `./data/demo/` and change file path in `./tools/demo_graspRGD.py`
46+
47+
48+
49+
### Train
50+
51+
1. Generate data
52+
1-1. Download [Cornell Dataset](http://pr.cs.cornell.edu/grasping/rect_data/data.php)
53+
1-2. Run `dataPreprocessingTest_fasterrcnn_split.m` (please modify paths according to your structure)
54+
2. Train
55+
56+
```
57+
./experiments/scripts/train_faster_rcnn.sh 0 graspRGB res50
58+
```
59+
60+
61+
62+
63+
64+
### Thanks
65+
66+
[GitHub - ivalab/grasp_multiObject_multiGrasp: An implementation of our RA-L work 'Real-world Multi-object, Multi-grasp Detection'](https://github.com/ivalab/grasp_multiObject_multiGrasp)
76.9 KB
Loading
76.2 KB
Loading
81.9 KB
Loading
76 KB
Loading
77.8 KB
Loading
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
%% script to test dataPreprocessing
2+
%% created by Fu-Jen Chu on 09/15/2016
3+
4+
close all
5+
clear
6+
7+
%parpool(4)
8+
addpath('/media/fujenchu/home3/data/grasps/')
9+
10+
11+
% generate list for splits
12+
list = [100:949 1000:1034];
13+
list_idx = randperm(length(list));
14+
train_list_idx = list_idx(length(list)/5+1:end);
15+
test_list_idx = list_idx(1:length(list)/5);
16+
train_list = list(train_list_idx);
17+
test_list = list(test_list_idx);
18+
19+
20+
for folder = 1:10
21+
display(['processing folder ' int2str(folder)])
22+
23+
imgDataDir = ['/home/fujenchu/projects/deepLearning/tensorflow-finetune-flickr-style-master/data/grasps/' sprintf('%02d',folder) '_rgd'];
24+
txtDataDir = ['/home/fujenchu/projects/deepLearning/tensorflow-finetune-flickr-style-master/data/grasps/' sprintf('%02d',folder)];
25+
26+
%imgDataOutDir = ['/home/fujenchu/projects/deepLearning/tensorflow-finetune-flickr-style-master/data/grasps/' sprintf('%02d',folder) '_Cropped320_rgd'];
27+
imgDataOutDir = '/media/fujenchu/home3/fasterrcnn_grasp/rgd_multibbs_5_5_5_tf/data/Images';
28+
annotationDataOutDir = '/media/fujenchu/home3/fasterrcnn_grasp/rgd_multibbs_5_5_5_tf/data/Annotations';
29+
imgSetTrain = '/media/fujenchu/home3/fasterrcnn_grasp/rgd_multibbs_5_5_5_tf/data/ImageSets/train.txt';
30+
imgSetTest = '/media/fujenchu/home3/fasterrcnn_grasp/rgd_multibbs_5_5_5_tf/data/ImageSets/test.txt';
31+
32+
imgFiles = dir([imgDataDir '/*.png']);
33+
txtFiles = dir([txtDataDir '/*pos.txt']);
34+
35+
logfileID = fopen('log.txt','a');
36+
mainfileID = fopen(['/home/fujenchu/projects/deepLearning/deepGraspExtensiveOffline/data/grasps/scripts/trainttt' sprintf('%02d',folder) '.txt'],'a');
37+
for idx = 1:length(imgFiles)
38+
%% display progress
39+
tic
40+
display(['processing folder: ' sprintf('%02d',folder) ', imgFiles: ' int2str(idx)])
41+
42+
%% reading data
43+
imgName = imgFiles(idx).name;
44+
[pathstr,imgname] = fileparts(imgName);
45+
46+
filenum = str2num(imgname(4:7));
47+
if(any(test_list == filenum))
48+
file_writeID = fopen(imgSetTest,'a');
49+
fprintf(file_writeID, '%s\n', [imgDataDir(1:end-3) 'Cropped320_rgd/' imgname '_preprocessed_1.png' ] );
50+
fclose(file_writeID);
51+
continue;
52+
end
53+
54+
txtName = txtFiles(idx).name;
55+
[pathstr,txtname] = fileparts(txtName);
56+
57+
img = imread([imgDataDir '/' imgname '.png']);
58+
fileID = fopen([txtDataDir '/' txtname '.txt'],'r');
59+
sizeA = [2 100];
60+
bbsIn_all = fscanf(fileID, '%f %f', sizeA);
61+
fclose(fileID);
62+
63+
%% data pre-processing
64+
[imagesOut bbsOut] = dataPreprocessing_fasterrcnn(img, bbsIn_all, 227, 5, 5);
65+
66+
% for each augmented image
67+
for i = 1:1:size(imagesOut,2)
68+
69+
% for each bbs
70+
file_writeID = fopen([annotationDataOutDir '/' imgname '_preprocessed_' int2str(i) '.txt'],'w');
71+
printCount = 0;
72+
for ibbs = 1:1:size(bbsOut{i},2)
73+
A = bbsOut{i}{ibbs};
74+
xy_ctr = sum(A,2)/4; x_ctr = xy_ctr(1); y_ctr = xy_ctr(2);
75+
width = sqrt(sum((A(:,1) - A(:,2)).^2)); height = sqrt(sum((A(:,2) - A(:,3)).^2));
76+
if(A(1,1) > A(1,2))
77+
theta = atan((A(2,2)-A(2,1))/(A(1,1)-A(1,2)));
78+
else
79+
theta = atan((A(2,1)-A(2,2))/(A(1,2)-A(1,1))); % note y is facing down
80+
end
81+
82+
% process to fasterrcnn
83+
x_min = x_ctr - width/2; x_max = x_ctr + width/2;
84+
y_min = y_ctr - height/2; y_max = y_ctr + height/2;
85+
%if(x_min < 0 || y_min < 0 || x_max > 227 || y_max > 227) display('yoooooooo'); end
86+
if((x_min < 0 && x_max < 0) || (y_min > 227 && y_max > 227) || (x_min > 227 && x_max > 227) || (y_min < 0 && y_max < 0)) display('xxxxxxxxx'); break; end
87+
cls = round((theta/pi*180+90)/10) + 1;
88+
89+
% write as lefttop rightdown, Xmin Ymin Xmax Ymax, ex: 261 109 511 705 (x水平 y垂直)
90+
fprintf(file_writeID, '%d %f %f %f %f\n', cls, x_min, y_min, x_max, y_max );
91+
printCount = printCount+1;
92+
end
93+
if(printCount == 0) fprintf(logfileID, '%s\n', [imgname '_preprocessed_' int2str(i) ]);end
94+
95+
fclose(file_writeID);
96+
imwrite(imagesOut{i}, [imgDataOutDir '/' imgname '_preprocessed_' int2str(i) '.png']);
97+
98+
% write filename to imageSet
99+
file_writeID = fopen(imgSetTrain,'a');
100+
fprintf(file_writeID, '%s\n', [imgname '_preprocessed_' int2str(i) ] );
101+
fclose(file_writeID);
102+
103+
end
104+
105+
106+
toc
107+
end
108+
fclose(mainfileID);
109+
110+
end
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
function [imagesOut bbsOut] = dataPreprocessing( imageIn, bbsIn_all, cropSize, translationShiftNumber, roatateAngleNumber)
2+
% dataPreprocessing function perfroms
3+
% 1) croping
4+
% 2) padding
5+
% 3) rotatation
6+
% 4) shifting
7+
%
8+
% for a input image with a bbs as 4 points,
9+
% dataPreprocessing outputs a set of images with corresponding bbs.
10+
%
11+
%
12+
% Inputs:
13+
% imageIn: input image (480 by 640 by 3)
14+
% bbsIn: bounding box (2 by 4)
15+
% cropSize: output image size
16+
% shift: shifting offset
17+
% rotate: rotation angle
18+
%
19+
% Outputs:
20+
% imagesOut: output images (n images)
21+
% bbsOut: output bbs according to shift and rotation
22+
%
23+
%% created by Fu-Jen Chu on 09/15/2016
24+
25+
debug_dev = 0;
26+
debug = 0;
27+
%% show image and bbs
28+
if(debug_dev)
29+
figure(1); imshow(imageIn); hold on;
30+
x = bbsIn_all(1, [1:3]);
31+
y = bbsIn_all(2, [1:3]);
32+
plot(x,y); hold off;
33+
end
34+
35+
%% crop image and padding image
36+
% cropping to 321 by 321 from center
37+
imgCrop = imcrop(imageIn, [145 65 351 351]);
38+
39+
% padding to 501 by 501
40+
imgPadding = padarray(imgCrop, [75 75], 'replicate', 'both');
41+
42+
count = 1;
43+
for i_rotate = 1:roatateAngleNumber*translationShiftNumber*translationShiftNumber
44+
% random roatateAngle
45+
theta = randi(360)-1;
46+
%theta = 0;
47+
48+
% random translationShift
49+
dx = randi(101)-51;
50+
%dx = 0;
51+
52+
%% rotation and shifting
53+
% random translationShift
54+
dy = randi(101)-51;
55+
%dy = 0;
56+
57+
imgRotate = imrotate(imgPadding, theta);
58+
if(debug_dev)figure(2); imshow(imgRotate);end
59+
imgCropRotate = imcrop(imgRotate, [size(imgRotate,1)/2-160-dx size(imgRotate,1)/2-160-dy 320 320]);
60+
if(debug_dev)figure(3); imshow(imgCropRotate);end
61+
imgResize = imresize(imgCropRotate, [cropSize cropSize]);
62+
if(debug)figure(4); imshow(imgResize); hold on;end
63+
64+
%% modify bbs
65+
[m, n] = size(bbsIn_all);
66+
bbsNum = n/4;
67+
68+
countbbs = 1;
69+
for idx = 1:bbsNum
70+
bbsIn = bbsIn_all(:,idx*4-3:idx*4);
71+
if(sum(sum(isnan(bbsIn)))) continue; end
72+
73+
bbsInShift = bbsIn - repmat([320; 240], 1, 4);
74+
R = [cos(theta/180*pi) -sin(theta/180*pi); sin(theta/180*pi) cos(theta/180*pi)];
75+
bbsRotated = (bbsInShift'*R)';
76+
bbsInShiftBack = (bbsRotated + repmat([160; 160], 1, 4) + repmat([dx; dy], 1, 4))*cropSize/320;
77+
if(debug)
78+
figure(4)
79+
x = bbsInShiftBack(1, [1:4 1]);
80+
y = bbsInShiftBack(2, [1:4 1]);
81+
plot(x,y); hold on; pause(0.01);
82+
end
83+
bbsOut{count}{countbbs} = bbsInShiftBack;
84+
countbbs = countbbs + 1;
85+
end
86+
87+
imagesOut{count} = imgResize;
88+
count = count +1;
89+
90+
91+
end
92+
93+
94+
95+
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
3+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/../" && pwd )"
4+
cd $DIR
5+
6+
NET=res101
7+
FILE=voc_0712_80k-110k.tgz
8+
# replace it with gs11655.sp.cs.cmu.edu if ladoga.graphics.cs.cmu.edu does not work
9+
URL=http://ladoga.graphics.cs.cmu.edu/xinleic/tf-faster-rcnn/$NET/$FILE
10+
CHECKSUM=cb32e9df553153d311cc5095b2f8c340
11+
12+
if [ -f $FILE ]; then
13+
echo "File already exists. Checking md5..."
14+
os=`uname -s`
15+
if [ "$os" = "Linux" ]; then
16+
checksum=`md5sum $FILE | awk '{ print $1 }'`
17+
elif [ "$os" = "Darwin" ]; then
18+
checksum=`cat $FILE | md5`
19+
fi
20+
if [ "$checksum" = "$CHECKSUM" ]; then
21+
echo "Checksum is correct. No need to download."
22+
exit 0
23+
else
24+
echo "Checksum is incorrect. Need to download again."
25+
fi
26+
fi
27+
28+
echo "Downloading Resnet 101 Faster R-CNN models Pret-trained on VOC 07+12 (340M)..."
29+
30+
wget $URL -O $FILE
31+
32+
echo "Unzipping..."
33+
34+
tar zxvf $FILE
35+
36+
echo "Done. Please run this command again to verify that checksum = $CHECKSUM."
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
EXP_DIR: res101-lg
2+
TRAIN:
3+
HAS_RPN: True
4+
IMS_PER_BATCH: 1
5+
BBOX_NORMALIZE_TARGETS_PRECOMPUTED: True
6+
RPN_POSITIVE_OVERLAP: 0.7
7+
RPN_BATCHSIZE: 256
8+
PROPOSAL_METHOD: gt
9+
BG_THRESH_LO: 0.0
10+
DISPLAY: 20
11+
BATCH_SIZE: 256
12+
WEIGHT_DECAY: 0.0001
13+
DOUBLE_BIAS: False
14+
SNAPSHOT_PREFIX: res101_faster_rcnn
15+
SCALES: [800]
16+
MAX_SIZE: 1333
17+
TEST:
18+
HAS_RPN: True
19+
SCALES: [800]
20+
MAX_SIZE: 1333
21+
RPN_POST_NMS_TOP_N: 1000
22+
POOLING_MODE: crop
23+
ANCHOR_SCALES: [2,4,8,16,32]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
EXP_DIR: res101
2+
TRAIN:
3+
HAS_RPN: True
4+
IMS_PER_BATCH: 1
5+
BBOX_NORMALIZE_TARGETS_PRECOMPUTED: True
6+
RPN_POSITIVE_OVERLAP: 0.7
7+
RPN_BATCHSIZE: 256
8+
PROPOSAL_METHOD: gt
9+
BG_THRESH_LO: 0.0
10+
DISPLAY: 20
11+
BATCH_SIZE: 256
12+
WEIGHT_DECAY: 0.0001
13+
DOUBLE_BIAS: False
14+
SNAPSHOT_PREFIX: res101_faster_rcnn
15+
TEST:
16+
HAS_RPN: True
17+
POOLING_MODE: crop
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
EXP_DIR: res50
2+
TRAIN:
3+
HAS_RPN: True
4+
IMS_PER_BATCH: 1
5+
BBOX_NORMALIZE_TARGETS_PRECOMPUTED: True
6+
RPN_POSITIVE_OVERLAP: 0.7
7+
RPN_BATCHSIZE: 256
8+
PROPOSAL_METHOD: gt
9+
BG_THRESH_LO: 0.0
10+
DISPLAY: 20
11+
BATCH_SIZE: 256
12+
WEIGHT_DECAY: 0.0001
13+
DOUBLE_BIAS: False
14+
SNAPSHOT_PREFIX: res50_faster_rcnn
15+
TEST:
16+
HAS_RPN: True
17+
POOLING_MODE: crop
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
EXP_DIR: vgg16
2+
TRAIN:
3+
HAS_RPN: True
4+
IMS_PER_BATCH: 1
5+
BBOX_NORMALIZE_TARGETS_PRECOMPUTED: True
6+
RPN_POSITIVE_OVERLAP: 0.7
7+
RPN_BATCHSIZE: 256
8+
PROPOSAL_METHOD: gt
9+
BG_THRESH_LO: 0.0
10+
DISPLAY: 20
11+
BATCH_SIZE: 256
12+
SNAPSHOT_PREFIX: vgg16_faster_rcnn
13+
TEST:
14+
HAS_RPN: True
15+
POOLING_MODE: crop

0 commit comments

Comments
 (0)