satnogs-wut/wut-ml

152 lines
4.0 KiB
Plaintext
Raw Normal View History

2020-01-01 23:24:08 -07:00
#!/usr/bin/python3
2020-01-02 16:07:25 -07:00
# wut-ml
2020-01-02 22:51:51 -07:00
#
# Vet a SatNOGS image using machine learning (guessing).
# It will vet the image located at test/unvetted/waterfall.png.
#
# Note, there is an issue to fix where it will vet everything
2020-01-03 18:13:37 -07:00
# under the data/test directory, so fix that. For now, just delete
2020-01-02 22:51:51 -07:00
# everything else. :)
#
# Usage:
# wut-ml
# Example:
# wut-ml
2020-01-02 11:53:15 -07:00
2020-01-01 23:24:08 -07:00
import os
import numpy as np
import tensorflow.python.keras
from tensorflow.python.keras import Sequential
from tensorflow.python.keras.layers import Activation, Dropout, Flatten, Dense
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
from tensorflow.python.keras.layers import Convolution2D, MaxPooling2D, ZeroPadding2D
from tensorflow.python.keras import optimizers
from tensorflow.python.keras.preprocessing import image
from tensorflow.python.keras.models import load_model
from tensorflow.python.keras.preprocessing.image import load_img
from tensorflow.python.keras.preprocessing.image import img_to_array
2020-01-04 15:40:46 -07:00
# https://keras.io/preprocessing/image/
print("datagen")
2020-01-04 15:19:22 -07:00
datagen = ImageDataGenerator(
featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
zca_epsilon=1e-06,
rescale=1./255,
shear_range=0.0,
zoom_range=0.0,
rotation_range=0,
width_shift_range=0.0,
height_shift_range=0.0,
brightness_range=None,
channel_shift_range=0.0,
fill_mode='nearest',
cval=0.0,
horizontal_flip=False,
vertical_flip=False,
preprocessing_function=None,
data_format='channels_last',
validation_split=0.0,
dtype='float32')
2020-01-04 15:40:46 -07:00
print("datagen.flow")
2020-01-01 23:24:08 -07:00
train_it = datagen.flow_from_directory('data/train/', class_mode='binary')
2020-01-02 19:28:26 -07:00
val_it = datagen.flow_from_directory('data/val/', class_mode='binary')
2020-01-01 23:24:08 -07:00
test_it = datagen.flow_from_directory('data/test/', class_mode='binary')
2020-01-04 15:40:46 -07:00
print("train_it.next()")
2020-01-01 23:24:08 -07:00
batchX, batchy = train_it.next()
2020-01-02 11:48:06 -07:00
print('Batch shape=%s, min=%.3f, max=%.3f' % (batchX.shape, batchX.min(), batchX.max()))
2020-01-04 14:12:51 -07:00
#img_width=823
#img_height=1606
2020-01-01 23:24:08 -07:00
img_width=256
img_height=256
2020-01-04 15:40:46 -07:00
print("Height", img_height, "Width", img_width)
# https://keras.io/models/sequential/
# https://keras.io/getting-started/sequential-model-guide/
print("Sequential")
2020-01-01 23:24:08 -07:00
model = Sequential()
2020-01-04 15:40:46 -07:00
print("add")
# https://keras.io/layers/convolutional/
2020-01-01 23:24:08 -07:00
model.add(Convolution2D(32, 3, 3, input_shape=(img_width, img_height,3)))
2020-01-04 15:40:46 -07:00
# https://keras.io/activations/
2020-01-01 23:24:08 -07:00
model.add(Activation('relu'))
2020-01-04 15:40:46 -07:00
# https://keras.io/layers/pooling/
2020-01-01 23:24:08 -07:00
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(64, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
2020-01-04 15:40:46 -07:00
# https://keras.io/layers/core/
2020-01-01 23:24:08 -07:00
model.add(Flatten())
2020-01-04 15:40:46 -07:00
# https://keras.io/layers/core/
2020-01-01 23:24:08 -07:00
model.add(Dense(64))
model.add(Activation('relu'))
2020-01-04 15:40:46 -07:00
# https://keras.io/layers/core/
2020-01-01 23:24:08 -07:00
model.add(Dropout(0.5))
model.add(Dense(1))
2020-01-04 13:35:09 -07:00
model.add(Activation('softmax'))
2020-01-04 15:40:46 -07:00
# https://keras.io/models/sequential/
print("compile")
2020-01-04 15:19:22 -07:00
model.compile(
loss='categorical_crossentropy',
loss_weights=None,
sample_weight_mode=None,
weighted_metrics=None,
target_tensors=None,
optimizer='rmsprop',
metrics=['accuracy'])
2020-01-04 15:40:46 -07:00
# https://keras.io/models/sequential/
print("fit")
2020-01-04 15:19:22 -07:00
model.fit(
x=train_it,
y=None,
2020-01-04 15:40:46 -07:00
batch_size=None,
epochs=1,
2020-01-04 15:19:22 -07:00
verbose=2,
2020-01-04 15:40:46 -07:00
callbacks=None,
validation_split=0.0,
validation_data=val_it,
shuffle=True,
class_weight=None,
sample_weight=None,
initial_epoch=0,
steps_per_epoch=None,
validation_steps=None,
validation_freq=1,
max_queue_size=10,
2020-01-04 15:19:22 -07:00
workers=16,
use_multiprocessing=True)
2020-01-04 15:40:46 -07:00
# https://keras.io/models/sequential/
2020-01-04 15:19:22 -07:00
# evaluate(x=None, y=None, batch_size=None, verbose=1, sample_weight=None, steps=None, callbacks=None, max_queue_size=10, workers=1, use_multiprocessing=False)
2020-01-01 23:24:08 -07:00
2020-01-04 15:40:46 -07:00
# https://keras.io/models/sequential/
print("predict")
2020-01-04 15:19:22 -07:00
prediction = model.predict(
x=test_it,
batch_size=None,
verbose=2,
steps=None,
callbacks=None,
max_queue_size=10,
workers=16,
use_multiprocessing=True)
2020-01-03 23:42:31 -07:00
2020-01-02 11:48:06 -07:00
print(prediction)
2020-01-01 23:24:08 -07:00
if prediction[0][0] == 1:
rating = 'bad'
2020-01-02 20:56:16 -07:00
else:
rating = 'good'
2020-01-01 23:24:08 -07:00
print('Observation: %s' % (rating))