卫星图片分类(ipynb

#%%

import tensorflow as tf

#%%

print('Tensorflow version: {}'.format(tf.__version__))

#%%

from tensorflow import keras
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import glob

#%%

all_image_path = glob.glob('../2_class/*/*.jpg')

#%%

all_image_path[:3]

#%%

all_image_path[-3:]

#%%

import random
random.shuffle(all_image_path)

#%%

all_image_path[:3]

#%%

label_to_index = {'airplane': 0,  'lake': 1}

#%%

index_to_label = dict((v, k) for k, v in label_to_index.items())

#%%

index_to_label

#%%

a_path = all_image_path[100]
a_path

#%%

a_path.split('\\')[-1].split('_')[0]

#%%

all_labels = [label_to_index.get(p.split('\\')[-1].split('_')[0]) 
                                                  for p in all_image_path]

#%%

all_labels[:3]

#%%

all_labels[-3:]

#%%

all_image_path[:3]

#%% md

加载和格式化图像

#%%

img_path = all_image_path[0]
img_path

#%%

img_raw = tf.io.read_file(img_path)

#%%

img_raw

#%%

img_tensor = tf.image.decode_jpeg(img_raw)

print(img_tensor.shape)
print(img_tensor.dtype)

#%%

img_tensor = tf.cast(img_tensor, tf.float32)
img_tensor = img_tensor/255.0
print(img_tensor.shape)
print(img_tensor.numpy().min())
print(img_tensor.numpy().max())

#%%

def load_and_preprocess_image(path):
    image = tf.io.read_file(path)
    image = tf.image.decode_jpeg(image, channels=3)
    image = tf.image.resize(image, [256, 256])
    image = tf.cast(image, tf.float32)
    image = image/255.0  # normalize to [0,1] range
    return image

#%%

i = random.choice(range(len(all_image_path)))
image_path = all_image_path[i]
label = all_labels[i]

img_tensor = load_and_preprocess_image(image_path)
plt.imshow(img_tensor.numpy())
plt.xlabel(index_to_label.get(label))

#%%

img_ds = tf.data.Dataset.from_tensor_slices(all_image_path)

#%%

AUTOTUNE = tf.data.experimental.AUTOTUNE
img_ds = img_ds.map(load_and_preprocess_image, num_parallel_calls=AUTOTUNE)

#%%

label_ds = tf.data.Dataset.from_tensor_slices(all_labels)

#%%

for label in label_ds.take(10):
    print(index_to_label[label.numpy()])

#%%

image_label_ds = tf.data.Dataset.zip((img_ds, label_ds))

#%%

image_label_ds

#%%

image_count = len(all_image_path)

#%%

test_count = int(image_count*0.2)
train_count = image_count - test_count

#%%

train_ds = image_label_ds.skip(test_count)
test_ds = image_label_ds.take(test_count)

#%%

BATCH_SIZE = 16

#%%

train_ds = train_ds.repeat().shuffle(buffer_size=train_count).batch(BATCH_SIZE)
train_ds

#%%

test_ds = test_ds.batch(BATCH_SIZE)

#%% md

建立模型

#%%

model = tf.keras.Sequential()   #顺序模型
model.add(tf.keras.layers.Conv2D(64, (3, 3), input_shape=(256, 256, 3), activation='relu'))
model.add(tf.keras.layers.Conv2D(64, (3, 3), activation='relu'))
model.add(tf.keras.layers.MaxPooling2D())
model.add(tf.keras.layers.Conv2D(128, (3, 3), activation='relu'))
model.add(tf.keras.layers.Conv2D(128, (3, 3), activation='relu'))
model.add(tf.keras.layers.MaxPooling2D())
model.add(tf.keras.layers.Conv2D(256, (3, 3), activation='relu'))
model.add(tf.keras.layers.Conv2D(256, (3, 3), activation='relu'))
model.add(tf.keras.layers.MaxPooling2D())
model.add(tf.keras.layers.Conv2D(512, (3, 3), activation='relu'))
model.add(tf.keras.layers.Conv2D(512, (3, 3), activation='relu'))
model.add(tf.keras.layers.MaxPooling2D())
model.add(tf.keras.layers.Conv2D(512, (3, 3), activation='relu'))
model.add(tf.keras.layers.Conv2D(512, (3, 3), activation='relu'))
model.add(tf.keras.layers.Conv2D(512, (3, 3), activation='relu'))
model.add(tf.keras.layers.GlobalAveragePooling2D())
model.add(tf.keras.layers.Dense(1024, activation='relu'))
model.add(tf.keras.layers.Dense(256, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))

#%%

model.summary()

#%%

model.compile(optimizer=tf.keras.optimizers.Adam(0.0001),
              loss=tf.keras.losses.BinaryCrossentropy(),
              metrics=['acc']
)

#%%

steps_per_epoch = train_count//BATCH_SIZE
validation_steps = test_count//BATCH_SIZE

#%%

history = model.fit(train_ds, epochs=10, 
                    steps_per_epoch=steps_per_epoch, 
                    validation_data=test_ds, 
                    validation_steps=validation_steps)

#%%

history.history.keys()

#%%

plt.plot(history.epoch, history.history.get('acc'), label='acc')
plt.plot(history.epoch, history.history.get('val_acc'), label='val_acc')
plt.legend()

#%%

plt.plot(history.epoch, history.history.get('loss'), label='loss')
plt.plot(history.epoch, history.history.get('val_loss'), label='val_loss')
plt.legend()

#%% md

使用模型预测

#%% raw
model.predict()
#%%

img_path = 'test02.jpg'
img = load_and_preprocess_image(img_path)

#%%

img.shape

#%%

img = tf.expand_dims(img, axis=0)

#%%

img.shape

#%%

pred = model.predict(img)

#%%

index_to_label.get((pred > 0.5).astype('int')[0][0])

#%%

def pre_img(img_path):
    img = load_and_preprocess_image(img_path)
    img = tf.expand_dims(img, axis=0)
    pred = model.predict(img)
    print(index_to_label.get((pred > 0.5).astype('int')[0][0]))

#%%

pre_img('test01.jpg')

#%%

pre_img('test02.jpg')

#%%



你可能感兴趣的:(机器学习)