2023-11-30 00:45:04 +00:00
|
|
|
import tensorflow as tf
|
|
|
|
from tensorflow import keras
|
2023-11-30 01:09:07 +00:00
|
|
|
import requests
|
2023-11-30 00:45:04 +00:00
|
|
|
import numpy as np
|
|
|
|
import os
|
|
|
|
# Version Information
|
|
|
|
# tensorflow 2.2.0 , Cudnn7.6.5 and Cuda 10.1 , python 3.8
|
|
|
|
from keras import backend as K
|
|
|
|
K.clear_session()
|
|
|
|
|
|
|
|
gpus = tf.config.experimental.list_physical_devices('GPU')
|
|
|
|
if gpus:
|
|
|
|
try:
|
|
|
|
tf.config.experimental.set_virtual_device_configuration(gpus[0], [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=6024)])
|
|
|
|
except RuntimeError as e:
|
|
|
|
print(e)
|
|
|
|
os.exit(1)
|
|
|
|
|
|
|
|
print(tf.config.experimental.list_physical_devices())
|
|
|
|
|
|
|
|
print(tf.__version__)
|
|
|
|
|
|
|
|
print(tf.test.is_built_with_cuda())
|
|
|
|
|
|
|
|
(X_train, y_train), (X_test,y_test) = tf.keras.datasets.cifar10.load_data()
|
|
|
|
|
|
|
|
(X_train, y_train), (X_test,y_test) = tf.keras.datasets.cifar10.load_data()
|
|
|
|
|
2023-11-30 00:58:35 +00:00
|
|
|
print(X_train.shape,y_train.shape)
|
|
|
|
|
|
|
|
classes = ["airplane","automobile","bird","cat","deer","dog","frog","horse","ship","truck"]
|
|
|
|
|
|
|
|
print(classes[y_train[3][0]])
|
|
|
|
|
|
|
|
print("pre processing: scale images")
|
|
|
|
|
|
|
|
X_train_scaled = X_train / 255
|
|
|
|
X_test_scaled = X_test / 255
|
|
|
|
|
|
|
|
y_train_categorical = keras.utils.to_categorical(
|
|
|
|
y_train, num_classes=10, dtype='float32'
|
|
|
|
)
|
|
|
|
y_test_categorical = keras.utils.to_categorical(
|
|
|
|
y_test, num_classes=10, dtype='float32'
|
|
|
|
)
|
|
|
|
|
|
|
|
print("model build")
|
|
|
|
|
|
|
|
|
|
|
|
def get_model():
|
|
|
|
model = keras.Sequential([
|
|
|
|
keras.layers.Flatten(input_shape=(32,32,3)),
|
|
|
|
keras.layers.Dense(3000, activation='relu'),
|
|
|
|
keras.layers.Dense(1000, activation='relu'),
|
|
|
|
keras.layers.Dense(10, activation='sigmoid')
|
|
|
|
])
|
|
|
|
|
|
|
|
model.compile(optimizer='SGD',
|
|
|
|
loss='categorical_crossentropy',
|
|
|
|
metrics=['accuracy'])
|
|
|
|
return model
|
|
|
|
|
|
|
|
with tf.device('/GPU:0'):
|
2023-11-30 01:04:15 +00:00
|
|
|
model = keras.Sequential([
|
|
|
|
keras.layers.Flatten(input_shape=(32,32,3)),
|
|
|
|
keras.layers.Dense(3000, activation='relu'),
|
|
|
|
keras.layers.Dense(1000, activation='relu'),
|
|
|
|
keras.layers.Dense(10, activation='sigmoid')
|
|
|
|
])
|
2023-11-30 11:20:22 +00:00
|
|
|
# g
|
2023-11-30 01:04:15 +00:00
|
|
|
model.compile(optimizer='SGD',
|
|
|
|
loss='categorical_crossentropy',
|
|
|
|
metrics=['accuracy'])
|
2024-04-11 07:07:42 +00:00
|
|
|
model.fit(X_train_scaled, y_train_categorical, epochs=50)
|
2023-11-30 01:04:15 +00:00
|
|
|
model.save('mymodel.keras')
|
2023-11-30 00:58:35 +00:00
|
|
|
|
2023-11-30 01:09:07 +00:00
|
|
|
print("finished training")
|
|
|
|
|
|
|
|
myurl = 'https://share.storage.sandbox.iuk.hdm-stuttgart.de/upload'
|
|
|
|
|
|
|
|
print("uploading file")
|
|
|
|
files = {
|
|
|
|
'fileUpload':('mymodel.keras', open('mymodel.keras', 'rb'),'application/octet-stream')
|
|
|
|
}
|
|
|
|
|
|
|
|
response = requests.post(myurl, files=files)
|
|
|
|
print(response,response.text)
|
|
|
|
|
2023-11-30 00:58:35 +00:00
|
|
|
print("done")
|