LoginSignup
2
4

More than 3 years have passed since last update.

Tensorflowで自作のデータセットで学習する

Posted at

Keras公式のデータセットの構造を確認

test
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

ダウンロードすると実体は下記に保存される

~/.keras/datasets/fashion-mnist/

自作データセット用の画像を準備する

Jpegで手頃な大きさのものを用意

用意した画像を読み込んで、学習できる配列にする

4枚の画像を読み込んでみる。
np.arrayで変換しなきゃいけないのはみんなハマるようなので要注意!

エラーの例
ValueError: Failed to find data adapter that can handle input: <class 'numpy.ndarray'>, (<class 'list'> containing values of types {"<class 'int'>"})
#ラベル
train_labels = [0, 0, 1, 1]
train_labels = np.array(train_labels) #np.array形式にしないと読み込めないので

#画像を読み込む
train_images = []
for i in range(4):
    print("/img/img"+str(i+1)+".jpeg")
    img_path = "/img/img"+str(i+1)+".jpeg"
    img_raw = tf.io.read_file(img_path)
    img_tensor = tf.image.decode_image(img_raw)
    img_final = tf.image.resize(img_tensor, [28, 28])
    img_gray = tf.image.rgb_to_grayscale(img_final)
    img_squeeze = tf.squeeze(img_gray)
    train_images.append(img_squeeze) #配列に追加していく

train_images = np.array(train_images) #np.array形式にしないと読み込めないので
train_images = train_images / 255.0

あれ、、あんまり正しく判定できてない。学習データがよくないかも。。
画像処理で二値化とか、境界線くっきりとかした方がよいか。

出力層が2だと判定ミス、10だと正解。ただ差が明確についてない。

2
4
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
2
4