View ship_detection_library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json, sys, random | |
import numpy as np | |
import tensorflow as tf | |
import tensorflow.keras | |
from tensorflow.keras.models import Sequential | |
from tensorflow.keras.layers import Dense, Flatten, Activation | |
from tensorflow.keras.layers import Dropout | |
from tensorflow.keras.layers import Conv2D, MaxPooling2D | |
from tensorflow.keras.utils import to_categorical | |
from tensorflow.keras.optimizers import SGD |
View ship_detection_read_dataset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# download dataset from json object | |
f = open(r'./ships-in-satellite-imagery/shipsnet.json') #download at http://vipnas.buitrongan.com:5000/sharing/Gd1gOIi9A | |
dataset = json.load(f) | |
f.close() | |
input_data = np.array(dataset['data']).astype('uint8') | |
output_data = np.array(dataset['labels']).astype('uint8') | |
input_data.shape |
View ship_detection_preparedata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# output encoding | |
y = tensorflow.keras.utils.to_categorical(output_data, 2) | |
# shuffle all indexes | |
indexes = np.arange(2800) | |
np.random.shuffle(indexes) | |
X_train = X[indexes].transpose([0,2,3,1]) | |
y_train = y[indexes] |
View ship_detection_physical_memory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
physical_devices = tf.config.experimental.list_physical_devices('GPU') | |
for physical_device in physical_devices: | |
tf.config.experimental.set_memory_growth(physical_device, True) |
View ship_detection_cnn_model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model = Sequential() | |
model.add(Conv2D(32, (3, 3), padding='same', input_shape=(80, 80, 3), activation='relu')) | |
model.add(MaxPooling2D(pool_size=(2, 2))) #40x40 | |
model.add(Dropout(0.25)) | |
model.add(Conv2D(32, (3, 3), padding='same', activation='relu')) | |
model.add(MaxPooling2D(pool_size=(2, 2))) #20x20 | |
model.add(Dropout(0.25)) |
View ship_detection_training
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# optimization setup | |
sgd = SGD(lr=0.01, momentum=0.9, nesterov=True) | |
model.compile( | |
loss='categorical_crossentropy', | |
optimizer=sgd, | |
metrics=['accuracy']) | |
# training | |
model.fit( | |
X_train, |
View ship_detection_test_img
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
image = Image.open('./ships-in-satellite-imagery/scenes/scenes/sfbay_1.png') | |
pix = image.load() | |
n_spectrum = 3 | |
width = image.size[0] | |
height = image.size[1] | |
# creat vector | |
picture_vector = [] | |
for chanel in range(n_spectrum): |
View ship_detection_cut_test_img
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def cutting(x, y): | |
area_study = np.arange(3*80*80).reshape(3, 80, 80) | |
for i in range(80): | |
for j in range(80): | |
area_study[0][i][j] = picture_tensor[0][y+i][x+j] | |
area_study[1][i][j] = picture_tensor[1][y+i][x+j] | |
area_study[2][i][j] = picture_tensor[2][y+i][x+j] | |
area_study = area_study.reshape([-1, 3, 80, 80]) | |
area_study = area_study.transpose([0,2,3,1]) | |
area_study = area_study / 255 |
View ship_detection_classify_block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
step = 10; coordinates = [] | |
for y in range(int((height-(80-step))/step)): | |
for x in range(int((width-(80-step))/step) ): | |
area = cutting(x*step, y*step) | |
result = model.predict(area) | |
if result[0][1] > 0.90 and not_near(x*step,y*step, 88, coordinates): | |
coordinates.append([[x*step, y*step], result]) | |
print(result) | |
plt.imshow(area[0]) | |
plt.show() |
View ship_detection_location
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for e in coordinates: | |
show_ship(e[0][0], e[0][1], e[1][0][1]) | |
picture_tensor = picture_tensor.transpose(1,2,0) | |
picture_tensor.shape | |
plt.figure(1, figsize = (15, 30)) | |
plt.subplot(3,1,1) | |
plt.imshow(picture_tensor) |
OlderNewer