Skip to content

Instantly share code, notes, and snippets.

@dolaameng
Created June 5, 2018 11:54
Show Gist options
  • Save dolaameng/5d53efbad29913efd0198ab08b1ee5ab to your computer and use it in GitHub Desktop.
Save dolaameng/5d53efbad29913efd0198ab08b1ee5ab to your computer and use it in GitHub Desktop.
classification for peta

Use PETA Dataset for Gender Detection

Steps

  1. Download dataset from here
  2. Uncompress the dataset to PETA, it should contain 10 subfolders like this
drwxrwxr-x 3 dola dola 4096 Oct 20  2014 3DPeS
drwxrwxr-x 3 dola dola 4096 Oct 20  2014 CAVIAR4REID
drwxrwxr-x 3 dola dola 4096 Oct 20  2014 CUHK
drwxrwxr-x 3 dola dola 4096 Oct 20  2014 GRID
drwxrwxr-x 3 dola dola 4096 Oct 20  2014 i-LID
drwxrwxr-x 3 dola dola 4096 Oct 20  2014 MIT
drwxrwxr-x 3 dola dola 4096 Oct 20  2014 PRID
drwxrwxr-x 3 dola dola 4096 Oct 20  2014 SARC3D
drwxrwxr-x 3 dola dola 4096 Oct 20  2014 TownCentre
drwxrwxr-x 3 dola dola 4096 Oct 20  2014 VIPeR
  1. organize the photos into two subfolders male and female by running
python generate_dataset.py --peta_dir ./PETA --train_dir ./train
  1. train with tensorflow.hub Inception v3
python ~/ws/hub/examples/image_retraining/retrain.py \
	--image_dir train \
	--output_graph model/gender_graph.pb \
	--output_labels model/gender_labels.txt \
	--how_many_training_steps 4000 \
  1. freeze the model with strip_unused and quantization
python ~/ws/tensorflow/tensorflow/python/tools/strip_unused.py \
	--input_graph=./model/gender_graph.pb \
	--output_graph=./model/stripped_gender_graph.pb \
	--input_node_names=Placeholder \
	--output_node_names=final_result \
	--input_binary=true

and

python ~/ws/tensorflow/tensorflow/tools/quantization/quantize_graph.py \
	--input=model/stripped_gender_graph.pb \
	--output_node_names=final_result \
	--output=model/quantized_stripped_gender_graph.pb \
	--mode=weights
  1. deploy to the mobile tf_classify app and run by changing the following in ClassifierActivity.java
private static final int INPUT_SIZE = 299; 
private static final int IMAGE_MEAN = 128; 
private static final float IMAGE_STD = 128; 
private static final String INPUT_NAME = "Placeholder"; 
private static final String OUTPUT_NAME = "final_result"; 
private static final String MODEL_FILE = "file:///android_asset/quantized_stripped_gender_graph.pb"; 
private static final String LABEL_FILE = 
"file:///android_asset/dog_retrained_labels.txt";  
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import os
import pandas as pd
from argparse import ArgumentParser
import shutil
import re
ARGV = None
def main():
# parsing
parser = ArgumentParser()
parser.add_argument(
'--peta_dir',
required=True,
type=str,
help='where peta data is located, including 10 subfolders'
)
parser.add_argument(
'--train_dir',
required=True,
type=str,
help='where training data (male/female folders) are generated'
)
ARGV, _ = parser.parse_known_args()
print(ARGV)
PETA_DIR = os.path.abspath(ARGV.peta_dir)
TRAIN_DIR = os.path.abspath(ARGV.train_dir)
# scaffolding
if os.path.exists(TRAIN_DIR):
shutil.rmtree(TRAIN_DIR)
os.makedirs(TRAIN_DIR)
os.makedirs(os.path.join(TRAIN_DIR, 'male'))
os.makedirs(os.path.join(TRAIN_DIR, 'female'))
# generating
for subdir in os.listdir(PETA_DIR):
if not os.path.isdir(os.path.join(PETA_DIR, subdir)): continue
print('Generating for {0}'.format(subdir))
subdir = os.path.join(PETA_DIR, subdir, 'archive')
lines = open(os.path.join(subdir, 'Label.txt')).readlines()
labels = []
for line in lines:
line = line.strip()
if line:
try:
label = (re.findall(r'^\d+', line)[0], re.findall(r'Male|Female', line)[0])
labels.append(label)
except:
print('ERROR', line)
male_head = '|'.join(set([head for head, gender in labels if gender == 'Male']))
female_head = '|'.join(set([head for head, gender in labels if gender == 'Female']))
for f in os.listdir(subdir):
if f.endswith('txt'): continue
src_f = os.path.join(subdir, f)
if re.match('^'+male_head, f):
dst_f = os.path.join(TRAIN_DIR, 'male', f)
elif re.match('^'+female_head, f):
dst_f = os.path.join(TRAIN_DIR, 'female', f)
shutil.copy(src_f, dst_f)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment