◀
戻る
進む
▶
🗒️
tensorflow URLを指定して画像を取得 Python3用
⇒#2288@研究ノート;
#HTTP
http://qiita.com…を参考に,python3に移植
import tensorflow as tf
import numpy as np
import requests
## 定数
IMG_SIZE = 256 ## 学習させる画像の縦幅・横幅
IMG_LENGTH = IMG_SIZE * IMG_SIZE * 3 ## 学習させる画像データ長
LABEL_CNT = 11 ## ラベルの種類の数
IMG_DOMAIN = 'ftp.yz.yamagata-u.ac.jp' ## 画像が取得できるURLのドメイン名
## 学習に必要な変数の初期化
x = tf.placeholder(tf.float32, shape=[None, IMG_LENGTH])
W = tf.Variable(tf.zeros([IMG_LENGTH, LABEL_CNT]))
b = tf.Variable(tf.zeros([LABEL_CNT]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, shape=[None, LABEL_CNT])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
## CSVファイルをワークキューとして設定
queue = tf.train.string_input_producer(['image.csv'])
reader = tf.TextLineReader()
key, val = reader.read(queue)
url, label = tf.decode_csv(val, [[''], [0]])
sess = tf.Session()
sess.run(tf.global_variables_initializer())
## バッチ処理の準備
batch_url, batch_label = tf.train.batch([url, label], batch_size=100)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
try :
while not coord.should_stop() :
urls, labels = sess.run([batch_url, batch_label])
for url in urls :
#con = http.client.HTTPConnection(IMG_DOMAIN)
#response = con.request('GET', "/pub/camera/AXIS_000/201708021519.jpg")
#r1 = response.getresponse()
#image = r1.read()
r=requests.get(url)
image = r.content
## 画像をTensorFlowで処理できるように変換
image = tf.image.decode_jpeg(image, channels=3)
image = tf.image.resize_image_with_crop_or_pad(image, IMG_SIZE, IMG_SIZE)
image = tf.reshape(image, [-1])
image = sess.run(image).astype(np.float32) / 255.0
for label in labels :
tmp = np.zeros(LABEL_CNT)
tmp[label] = 1
label = tmp
sess.run(train_step, feed_dict={x: [image], y_: [label]})
finally :
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess.run(accuracy, feed_dict={x: [image], y_: [label]})
coord.request_stop()
coord.join(threads)
西暦と元号
<a href="https://edu.yz.yamagata-u.ac.jp/developer/Asp/Youzan/Physics/Calendar.asp?date=2017-08-04">
<time datetime="2017-08-04">
2017-08-04
</time>
</a>
<a href="https://edu.yz.yamagata-u.ac.jp/developer/Asp/Youzan/Laboratory/LaboNote/@LaboNote.asp?id=2288">
tensorflow URLを指定して画像を取得 Python3用
</a>
🎄🎂🌃🕯🎉