LoginSignup
0
0

More than 5 years have passed since last update.

Caffeモデルの転移学習をやってみる

Posted at

ラベルには人工知能がラベル付けしたものを使います。人間によってラベリングされたデータは、許容度が大きく、「サングラスをかけた猫」「アニメの猫」も猫としてラベリングされているので、fine tuningは適さないのでやめておきましょう。

wget https://storage.googleapis.com/openimages/2017_07/images_2017_07.tar.gz
tar -zxvf images_2017_07.tar.gz
wget https://storage.googleapis.com/openimages/2017_07/annotations_machine_2017_07.tar.gz
tar -zxvf annotations_machine_2017_07.tar.gz

image.png

import csv
import os
import time
from PIL import Image
import urllib
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

all_tgt = ["cat", "dog", "person", "yard"]

for cur_tgt in all_tgt:
    print '\nClass : ' + str(cur_tgt) + '\n\n'
    f1 = open('/home/ubuntu/labels_'+str(cur_tgt)+'.csv', 'r')
    reader1 = csv.reader(f1)
    f2 = open('/home/ubuntu/2017_07/train/images.csv', 'r')
    reader2 = csv.reader(f2)
    confidence = 1.0
    if cur_tgt == 'yard':
        confidence = 0.9

    cnt = 0
    for row1 in reader1:

        if float(row1[3]) == confidence:

            for row2 in reader2:
                if row1[0] == row2[0]:
                    break
            print str(cnt)+' : '+row2[2]

            #load image
            try:
                urlimg = urllib.urlopen(row2[2])            
                img = Image.open(urlimg)
            except:
                continue # skip if error happened

            fname = '/home/ubuntu/downloaded_images/'+str(cur_tgt)+'.'+str(cnt)+'.jpg'
            if urlimg.url.find('unavailable') == -1:
                if cur_tgt != 'yard':
                    #resize image
                    ratio = img.width / 227.0
                    if img.height < img.width:
                        ratio = img.height / 227.0
                    img_resize = img.resize((int(round(img.width/ratio)), int(round(img.height/ratio))))
                    img_resize.save(fname)
                else:
                    img.save(fname)

                cnt = cnt + 1
                if 10000 <= cnt:
                    break
    f1.close()
    f2.close()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0