ninopira / study_tpu

0 stars 0 forks source link

りんごコンペ #2

Open ninopira opened 3 years ago

ninopira commented 3 years ago

対象コンペ

ninopira commented 3 years ago

ちょっとEDA

https://www.kaggle.com/itokashi/easy-eda

healthy                                                            516
multiple_diseases                                                   91
rust                                                               622
scab                                                               592

healthy

image

multiple_diseases

image

rust

オレンジの斑点 image

scab

茶色の斑点 image

ninopira commented 3 years ago

TPUチャレンジ1

これをベースにしてみる

元notebook: https://www.kaggle.com/xhlulu/plant-pathology-very-concise-tpu-efficientnet?scriptVersionId=32659758 自分のnotebook: https://www.kaggle.com/itokashi/very-concise-tpu-efficientnet-copy

写経+debug

作業ログ

TPU Config

tf.distribute.cluster_resolver.TPUClusterResolver()

Create Dataset objects

コメント

def decode_image(filename, label=None, image_size=(512, 512)):
    bits = tf.io.read_file(filename)
    image = tf.image.decode_jpeg(bits, channels=3)
    image = tf.cast(image, tf.float32) / 255.0
    image = tf.image.resize(image, image_size)

    if label is None:
        return image
    else:
        return image, label

def data_augment(image, label=None):
    image = tf.image.random_flip_left_right(image)
    image = tf.image.random_flip_up_down(image)

    if label is None:
        return image
    else:
        return image, label

コメント

train_dataset = (
    tf.data.Dataset
    .from_tensor_slices((train_paths, train_labels))
    .map(decode_image, num_parallel_calls=AUTO)
    .cache()
    .map(data_augment, num_parallel_calls=AUTO)
    .repeat()
    .shuffle(512)
    .batch(BATCH_SIZE)
    .prefetch(AUTO)
)

valid_dataset = (
    tf.data.Dataset
    .from_tensor_slices((valid_paths, valid_labels))
    .map(decode_image, num_parallel_calls=AUTO)
    .batch(BATCH_SIZE)
    .cache()
    .prefetch(AUTO)
)

test_dataset = (
    tf.data.Dataset
    .from_tensor_slices(test_paths)
    .map(decode_image, num_parallel_calls=AUTO)
    .batch(BATCH_SIZE)
)

modelのTPUへの読み込み

with strategy.scope():
    model = tf.keras.Sequential([
        efn.EfficientNetB7(
            input_shape=(512, 512, 3),
            weights='imagenet',
            include_top=False
        ),
        L.GlobalAveragePooling2D(),
        L.Dense(train_labels.shape[1], activation='softmax')
    ])

    model.compile(
        optimizer='adam',
        loss = 'categorical_crossentropy',
        metrics=['categorical_accuracy']
    )
    model.summary()

とりあえずTPUは掴んだっぽい ただいまいちフル活用できていないっぽそう image

https://www.kaggle.com/ateplyuk/fork-of-plant-2020-tpu-915e9c

感想

疑問・NEXT

その他参考