Skip to content

Commit

Permalink
GPU対応
Browse files Browse the repository at this point in the history
  • Loading branch information
abars committed Jan 2, 2019
1 parent cc05793 commit d97e506
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
19 changes: 18 additions & 1 deletion README.md
Expand Up @@ -10,7 +10,9 @@ Caffe

## Estimated Metadata

`identity_meta_with_estimated_age.csv`
You can use estimated output below

`output/identity_meta_with_estimated_age.csv`

## Estimation Tutorial

Expand All @@ -34,6 +36,19 @@ Download vggface2_test.tar.gz and vggface2_train.tar.gz and identity_meta.csv

http://www.robots.ox.ac.uk/~vgg/data/vgg_face2/

Untar downloaded dataset

`tar zxvf vggface2_train.tar.gz`
`tar zxvf vggface2_test.tar.gz`

This is a expected location

`dataset/vggface2/train`
`dataset/vggface2/test`
`dataset/vggface2/identity_meta.csv`

### Dataset format

identity_meta.csv includes these format

`Class_ID, Name, Sample_Num, Flag, Gender`
Expand All @@ -44,5 +59,7 @@ identity_meta.csv includes these format

This script add age label to identity_meta.csv

Output file is identity_meta_with_estimated_age.csv

`Class_ID, Name, Sample_Num, Flag, Gender, Age`

27 changes: 19 additions & 8 deletions estimate_age.py
Expand Up @@ -14,6 +14,7 @@
# ----------------------------------------------

DATASET_ROOT_PATH="/Volumes/TB4/Keras/"
DATASET_SUB_PATH="Dataset/vggface2/"

# ----------------------------------------------
# Argument
Expand All @@ -24,8 +25,16 @@
if len(sys.argv) >= 1:
if len(sys.argv) >= 2:
DATASET_ROOT_PATH=sys.argv[3]
if len(sys.argv) >= 3:
if sys.argv[4]=="gpu":
caffe.set_mode_gpu()
else:
if(sys.argv[4]=="cpu"):
None
else:
print("argv[4] must be gpu or cpu")
else:
print("usage: python calculate_age.py [datasetroot(optional)]")
print("usage: python calculate_age.py [datasetroot(optional)] [gpu/cpu(optional)]")
sys.exit(1)

# ----------------------------------------------
Expand Down Expand Up @@ -61,9 +70,9 @@ def calc_age(image):
# FileList
# ----------------------------------------------

lines=open(DATASET_ROOT_PATH+"Dataset/identity_meta.csv").readlines()
lines=open(DATASET_ROOT_PATH+DATASET_SUB_PATH+"identity_meta.csv").readlines()

OUTPUT_PATH="./identity_meta_with_estimated_age.csv"
OUTPUT_PATH=DATASET_ROOT_PATH+DATASET_SUB_PATH+"identity_meta_with_estimated_age.csv"
if(os.path.exists(OUTPUT_PATH)):
output_data=open(OUTPUT_PATH).readlines() #for continue
else:
Expand All @@ -76,27 +85,29 @@ def calc_age(image):
age=obj[5].strip()
cache[path]=age

DEBUG_MODE=1
DEBUG_MODE=0 #If 1, Age estimate from one image

with open(OUTPUT_PATH, mode='w') as f:
cnt=0
for line in lines:
obj=line.split(", ")
path=obj[0]
trainset=obj[3]

print("target person : "+path)
print("target person : "+path+" "+str(cnt)+"/"+str(len(lines)))
cnt=cnt+1

if path in cache.keys():
#already estimated
f.write(line.strip()+", "+str(cache[path])+"\n")
print(" use cached age "+str(cache[path]))
else:
#yet estimated
if trainset=="0":# or trainset=="1":
if trainset=="0" or trainset=="1":
if trainset=="0":
path2=DATASET_ROOT_PATH+"Dataset/test/"+path
path2=DATASET_ROOT_PATH+DATASET_SUB_PATH+"test/"+path
else:
path2=DATASET_ROOT_PATH+"Dataset/train/"+path
path2=DATASET_ROOT_PATH+DATASET_SUB_PATH+"train/"+path
age_sum=0
age_cnt=0
for image_path in glob.glob(path2+"/*.jpg"):
Expand Down

0 comments on commit d97e506

Please sign in to comment.