ããã¯, æ¨å¹´ã®10æï½12æã«éå¬ããããチームで学ぼう! TensorFlow(機械学習)実践編第2期ãã«ããã¦, ç§ã®åå ããããã¼ã 仲鶴å¾å(ä»®)ãã®ææãæ°åã«åãã¦ç´¹ä»ãããã®ã§ãã. (åå¼·ä¼ããããã¶ãæéãçµã£ã¦ãã¾ã£ãã....)
é¢é£è¨äº
ãããããã«ãèªèãã¦ã¿ãã! (1)
ペットボトルを認識してみよう! (2)
ペットボトルを認識してみよう! (3)
ペットボトルを認識してみよう! (4)
ペットボトルを認識してみよう! (5)
0. ã¡ã³ãã¼ç´¹ä»
ã¡ã³ãã¼ã¯ä»¥ä¸ã®4å.
åå | ã¹ãã«ãªã© |
仲ãã | çµã¿è¾¼ã¿ã½ããéçº, ãã¼ã¿åæ, ã·ã¹ãã è¨è¨, æ©æ¢°å¦ç¿, C, R, Python(åå¿è ) |
鶴ãâ¡ | ãã¿ã¼ã³èªè(é³å£°, æå), æ©æ¢°å¦ç¿ã®çµé¨ãã, 端æ«ã¢ããª, ããã«ã¦ã§ã¢éçº. C/C++, Android Java, Python(åå¿è ) |
å¾ãâ | ç©çå¦åºèº«, ã¬ã¼ãã®ä¿¡å·å¦çèããä»äº, æ©æ¢°å¦ç¿ç¬å¦3å¹´, Python3å¹´ |
åãâ³ | æ©æ¢°å¦ç¿ã®çµé¨ãªã, Cè¨èª, ããã°ã©ãæ´1å¹´ã¡ãã, Python(åå¿è ) |
ã¡ãªã¿ã«ãã¼ã åã®ç±æ¥ã§ããã, ããã¯ãã¼ã çºè¶³æã«ãã¼ã åãä»ããå¿ è¦ããã£ãã®ã ã, ã¡ã³ãã¼ã®èåã®ä¸åãçµã¿åããã¦ä»®ã®åãä»ãããã¨ãå§ã¾ãã§ãã.
1. ãã¼ãã¨ç®æ¨
åå¼·ä¼ã¯, ãTensorFlowãKerasãªã©ã®æ©æ¢°å¦ç¿ãã¼ã«ã使ã£ããµã¼ãã¹ãèã, å®è£
ãéãã¦æ©æ¢°å¦ç¿ãå¦ã¶.ãã¨ãããã¨ã§, åå è
ã6ãã¼ã (4-5å)ã«åãã, ãã¼ã ã§ããããä½ãããã決ããã¨ããããå§ã¾ã£ã.
æã
ã®ãã¼ã ã§ãã¢ã¤ãã¢åºãã®æ®µéã§ã¯ãããããªæ¡ãåºãã, å®éã«å®è£
ã試ããã¨ãèããã¨, ãã¼ã¿ãããç¨åº¦éãããããã¼ãã¨ããå¿
è¦ããã£ã.
ããã§æ±ºã¾ã£ããã¼ãã¨ç®æ¨ã¯ä»¥ä¸ã®éã.
ãã¼ã:
ããããããã«é£²æã®ã«ãã´ãªèªèã¨ãã®å¿ç¨ã
ã
ç®æ¨:
ãSTEP1 ã¡ã¼ã«ã¼ç»å(1æ¬/èæ¯ç½)ã§ã®å¤å¥
ãããã»ãã¼ã¿ã®åé, å種ç»åå¦çã«ãããã¼ã¿æ°´å¢ããã¼ã«ãä½æãã.
ãããã»TensorFlowã®ä½¿ãæ¹ãå¦ã¶
ãããã»CNNã«ããç»åèªèãã¨ãããããã£ã¦ã¿ã.
ãSTEP2 å®ç°å¢ä¸(è¤æ°æ¬/èæ¯ãã)ã§ã®å¤å¥
ãããã»å®ç°å¢ä¸ã§ã®ãã¼ã¿åéãè¡ã.
ãããã»ãããããã«åè£é åã®åãåºãã¨ãããããã«èªèãçµã¿åããã.
ãSTEP3 å¿ç¨
ãããã»ãããããã«èªèã®å¿ç¨ãèãã.
ãããã»å¿ç¨ä¾ãå®è£
ãã. ('16/12ã¾ã§ã«ã¯é£ããããª!?)
ã·ã¹ãã ã¤ã¡ã¼ã¸:
ãããããã«ã®ã«ãã´ãªèªèãéãã¦, TensorFlowã®ä½¿ãæ¹ãç»åèªèãã¹ããããè¸ãã§å¦ã³ãã£ã¦ã¿ããã¨ã«æ³¨åã, å¿ç¨ã«ã¤ãã¦ã¯æéãããã°ã¨ãããã¨ã«ãã¦æ´»åãéå§.
2. ãã¼ã¿åéã¨æ´å
2.1 ç»åãã¼ã¿åé
ãããããã«ã®ç»åã¨ãã®ååæ
å ±ãéããå¿
è¦ãããã, å¹ççã«ç»åãéããããã«, ã¾ãã¯å飲æã¡ã¼ã«ã¼ã®HPããéãããã¨ã«ãã.
ç»åã1æ1æãã¦ã³ãã¼ããã¦ããã®ã§ã¯ãã¼ã¿åéã«æéããããã®ã§,ååä¸è¦§ãã¼ã¸ããç»åãä¸æ¬åå¾ããç°¡åãªãã¼ã«ãä½æã, ä½æ¥ãå¹çå.
(1) ç»åURLã®ãªã¹ãä½æ
[ã³ã¼ã]
# -*- coding: utf-8 -*- import urllib #import urllib.request import chardet import os.path # from html.parser import HTMLParser from HTMLParser import HTMLParser class imgParser(HTMLParser): def __init__(self): HTMLParser.__init__(self) def handle_starttag(self,tagname,attribute): if tagname.lower() == "img": for i in attribute: if i[0].lower() == "src": # ãã¿ã³çã¯gifãå¤ãã®ã§é¤å» if '.gif' in i[1]: continue idx = i[1].find('?') if idx == -1: # ãµã¤ãºæå®ãªã img_url=i[1] else: img_url=i[1][:idx] # åå¾ããç»åã®URLãéãããã¡ã¤ã«ã®ä½æ f = open("collection_url.csv","a") # ãµã³ããªã¼ if input_num == 1: f.write("=IMAGE(\"{}\")\n".format(img_url)) # ã³ã«ã»ã³ã¼ã© elif input_num == 2: f.write("=IMAGE(\"http://www.cocacola.co.jp{}\")\n".format(img_url)) # ã¢ãµã飲æ elif input_num == 3: f.write("=IMAGE(\"http://www.asahiinryo.co.jp{}\")\n".format(img_url)) # ä»ï¼ãµã¤ãã«ãã£ã¦ãã¹ãéãï¼ls else: if img_url.startswith('http'): # httpã§å§ã¾ã f.write("=IMAGE(\"{}\")\n".format(img_url)) elif img_url.startswith('/'): # '/'ã§å§ã¾ã f.write("=IMAGE(\"" + serch_url + "{}\")\n".format(img_url)) else: f.write("=IMAGE(\"" + serch_url + "/{}\")\n".format(img_url)) f.close() if __name__ == "__main__": # ãã¡ã¤ã«ã®åé¤ if os.path.exists("collection_url.csv"): os.remove("collection_url.csv") print('ç»åãåå¾ãããURLãå ¥åãã¦ãã ããã') print('1: ãµã³ããªã¼\n2: ã³ã«ã»ã³ã¼ã©\n3: ã¢ãµã飲æ(çé ¸ã®ãã¼ã¸)\n4: ä»(ç´æ¥å ¥å)') input_num = input('>>> ') if input_num == 1: serch_url = 'http://www.suntory.co.jp/softdrink/products' elif input_num == 2: serch_url = 'http://www.cocacola.co.jp/brands/all-products' elif input_num == 3: serch_url = 'http://www.asahiinryo.co.jp/products/carbonated' else: print('URLãå ¥åãã¦ãã ããã') input_num = input('>>> ') serch_url = input_num print('ç»åURLãåå¾ä¸ã§ã...') # data = urllib.request.urlopen(serch_url).read() data = urllib.urlopen(serch_url).read() enc = chardet.detect(data) htmldata = data.decode(enc['encoding']) parser = imgParser() parser.feed(htmldata) parser.close() print("collection_url.csvãä½æãã¾ãã")
(2) URLãªã¹ãã®ç»åãã¦ã³ãã¼ã
[ã³ã¼ã]
# -*- coding: utf-8 -*- import urllib # import urllib.request import os.path import re def download(url,savename): # img = urllib.request.urlopen(url) img = urllib.urlopen(url) basename, ext = os.path.splitext(os.path.basename(url)) filename = savename + ext localfile = open(filename.strip(), 'wb') localfile.write(img.read()) img.close() localfile.close() def get_url_root(url): if("http://" in url): url_delet_http = url.lstrip("http://") if("/" in url_delet_http): url_root = "http://" + url_delet_http[0:url_delet_http.find("/")] return url_root elif("https://" in url): url_delet_http = url.lstrip("https://") if("/" in url_delet_http): url_root = "http://" + url_delet_http[0:url_delet_http.find("/")] return url_root return 0 if __name__ == "__main__": print('ç»åURLãªã¹ãã®ãã¡ã¤ã«åãå ¥åãã¦ãã ãã') print('./collection_url.csvã§ãããã°"1"ãå ¥åãã¦ãã ãã') input_filename = input('>>> ') if input_filename == 1: input_filename = 'collection_url.csv' print(input_filename) # çæãããã¡ã¤ã«ã®èªã¿è¾¼ã¿ f = open("{}".format(input_filename),"r") url_list = [] for row in f: # row_url = row.split('\t') --> csv ','åºåãå¯¾å¿ row_url = row.split(',') for k, u in enumerate(row_url): if u.startswith('=IMAGE'): u = re.sub('^=IMAGE\(\"', "", u) u = re.sub('\"\)$', "", u) url_list.append(u) len_url = len(url_list) f.close() print('{} file(s)'.format(len_url)) number_url = [] # for i in range(0,(len_url-1)): for i in range(0,(len_url)): number_url.append(url_list[i]) # for j in range(0,(len_url-1)): for j in range(0,(len_url)): url = number_url[j] if("../" in url): root_url = get_url_root(url) if(root_url!=0): url = url.replace("..",root_url) print(url) download(url, "asahi_{0:04d}".format(j)) print('ç»åã®ãã¦ã³ãã¼ããçµäºãã¾ãã')
2.2 ã©ããªã³ã°
ãããããã«ã®ã«ãã´ãªã¯ä»¥ä¸ã®11種ã¨ã, 2.1ã§åéããç»åãã¼ã¿ã¨å飲æã¡ã¼ã«ã¼ã®æ
å ±ãè¦ãªãã, 人æã§ä¸è¦ãªç»åã®é¤å»ã¨ã©ããªã³ã°ãè¡ã£ã.
[注] åç»åã¯å飲æã¡ã¼ã«ã¼ã®HPã®ãã®ã使ããã¦ããã ãã¦ãã¾ã.
ä¾ãã°ä¸è¨ã®ä¸é¨ã³ã¼ãã¼ã¨ã³ã¼ã©ã®ããã«, ããã¨è¦ç»åã ãã§ã¯ã©ã¡ããåããã«ãããã®ããããã¨ãåãã£ã.
ããã«å½¢ç¶ã¯ããªãé¡ä¼¼ãã¦ããã®ã§, 主ã«ããã«ã©ãã«ã飲ææ°´ã®è²ã§ã«ãã´ãªãèå¥ããã®ã§, ã«ãã´ãªæ°ã¯å°ãªããçµæ§é£ãããã...
åéãããããããã«ã®ç»åãã¼ã¿æ°ã¯ä»¥ä¸ã®éã. (æçµå ±åæç¹)
ä»åã¯, ããã¼ã¿åéã¨æ´åãã«ã¤ãã¦ç´¹ä»ããã, 次åã¯å¦ç¿ãã¼ã¿ãå¢ããããã®ãç»åã®æ°´å¢ãããªã©ã«ã¤ãã¦ç´¹ä»ããäºå®.
---
|
|
ãã¼ã¿ãéããæè¡ æéã§ä½ãã¹ã¯ã¬ã¤ãã³ã°&ã¯ãã¼ã©ã¼ (Informatics&IDEA)
|
Pythonã«ããWebã¹ã¯ã¬ã¤ãã³ã°
|