Skip to content
Snippets Groups Projects
Commit d00e97f6 authored by Michiel_VE's avatar Michiel_VE
Browse files

update what image is send

parent 668e8ce6
No related branches found
No related tags found
1 merge request!1made so code can be trained on GPU
......@@ -6,7 +6,7 @@ def plot_images(images, labels, class_indices):
num_images = len(images)
grid_size = int(np.ceil(np.sqrt(num_images)))
plt.figure(figsize=(15, 15))
plt.figure()
for i in range(num_images):
plt.subplot(grid_size, grid_size, i + 1)
img = images[i]
......
No preview for this file type
......@@ -43,85 +43,11 @@ jupyter notebook --notebook-dir='your working directory'
```
## Add your files
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
#### Drawing images
To see what images are used you can use matplot to draw them and verify
```
cd existing_repo
git remote add origin https://gitlab.labranet.jamk.fi/AC4908/thesis-idea.git
git branch -M main
git push -uf origin main
conda install matplotlib
```
## Integrate with your tools
- [ ] [Set up project integrations](https://gitlab.labranet.jamk.fi/AC4908/thesis-idea/-/settings/integrations)
## Collaborate with your team
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
## Test and Deploy
Use the built-in continuous integration in GitLab.
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
%% Cell type:code id:62c6c5c3-d2ba-4e79-b533-828c3083e8ea tags:
``` python
import time
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from keras.preprocessing.image import ImageDataGenerator
from keras.losses import CategoricalCrossentropy
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.optimizers import Adam
import tensorflow as tf
from Func.getSubFolders import count_sub_folders
from Func.DrawImages import plot_images
path = 'Data'
output = 'Model/keras_model.h5'
start_time = time.time()
physical_devices = tf.config.list_physical_devices('GPU')
print("Num GPUs Available: ", len(physical_devices))
if len(physical_devices) > 0:
try:
# Set memory growth to true
for device in physical_devices:
tf.config.experimental.set_memory_growth(device, True)
# Optionally, you can set a memory limit if necessary
# tf.config.set_logical_device_configuration(
# physical_devices[0],
# [tf.config.LogicalDeviceConfiguration(memory_limit=4096)]) # Set to 4GB
except RuntimeError as e:
print(e)
print('runtime gpu', e)
else:
print("No GPU available. Using CPU.")
# Step 1: Load and Preprocess Images
datagen = ImageDataGenerator(
rescale=1. / 255,
validation_split=0.2,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
)
test_datagen = ImageDataGenerator(rescale=1. / 255)
# Step 2: Label the Data
train_set = datagen.flow_from_directory(
path,
target_size=(300, 300),
batch_size=64,
target_size=(224, 224),
batch_size=16,
class_mode='categorical',
subset='training'
)
test_set = datagen.flow_from_directory(
path,
target_size=(300, 300),
batch_size=64,
target_size=(224, 224),
batch_size=16,
class_mode='categorical',
subset='validation'
)
# draw Images
# images, labels = next(test_set)
#images, labels = next(train_set)
# class_indices = test_set.class_indices
# plot_images(images, labels, class_indices)
#class_indices = test_set.class_indices
#plot_images(images, labels, class_indices)
# Step 4: Build the Model
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(300, 300, 3), activation='relu'))
model.add(Conv2D(32, (3, 3), input_shape=(224, 224, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(256, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(units=256, activation='relu'))
model.add(Dense(units=128, activation='relu'))
model.add(Dense(units=count_sub_folders(path), activation='softmax'))
# def createLayers(input_shape=(224, 224, 3)):
# inputs = tf.keras.Input(shape=input_shape)
#
# x = layers.Conv2D(48, (1, 1), padding='same', use_bias=False, name='block_1_expand')(inputs)
# x = layers.BatchNormalization(name='block_1_expand_BN')(x)
# x = layers.ReLU(6., name='block_1_expand_relu')(x)
#
# x = layers.DepthwiseConv2D((3, 3), padding='same', use_bias=False, name='block_1_depthwise')(x)
# x = layers.BatchNormalization(name='block_1_depthwise_BN')(x)
# x = layers.ReLU(6., name='block_1_depthwise_relu')(x)
#
# x = layers.Conv2D(8, (1, 1), padding='same', use_bias=False, name='block_1_project')(x)
# x = layers.BatchNormalization(name='block_1_project_BN')(x)
#
# for i in range(2,5):
# x1 = layers.Conv2D(48, (1, 1), padding='same', use_bias=False, name=f'block_{i}_expand')(x)
# x1 = layers.BatchNormalization(name=f'block_{i}_expand_BN')(x1)
# x1 = layers.ReLU(6., name=f'block_{i}_expand_relu')(x1)
#
# x1 = layers.DepthwiseConv2D((3, 3), padding='same', use_bias=False, name=f'block_{i}_depthwise')(x1)
# x1 = layers.BatchNormalization(name=f'block_{i}_depthwise_BN')(x1)
# x1 = layers.ReLU(6., name=f'block_{i}_depthwise_relu')(x1)
#
# x1 = layers.Conv2D(8, (1, 1), padding='same', use_bias=False, name=f'block_{i}_project')(x1)
# x1 = layers.BatchNormalization(name=f'block_{i}_project_BN')(x1)
#
# x = layers.Add(name=f'block_{i}_add')([x, x1])
#
# x = tf.keras.layers.GlobalAveragePooling2D()(x)
# outputs = tf.keras.layers.Dense(count_sub_folders(path), activation='softmax')(x)
# model = models.Model(inputs, outputs, name='testModel')
#
# return model
#
#
# model = createLayers()
# Compile the Model after pruning
model.compile(optimizer=Adam(learning_rate=0.001),
loss=CategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
# Step 6: Train the Model
early_stopping = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)
model.fit(train_set, validation_data=test_set, epochs=10, callbacks=early_stopping)
model.fit(train_set, validation_data=test_set, epochs=50, callbacks=early_stopping)
# Step 7: Evaluate the Model
loss, accuracy = model.evaluate(test_set)
print(f'Test loss: {loss}, Test accuracy: {accuracy}')
# Save the trained model
model.save(output)
end_time = time.time()
execute_time = (end_time - start_time) / 60
model.summary()
# Print the result
print(f"It took: {execute_time:0.2f} minutes")
```
%% Output
Num GPUs Available: 1
Physical devices cannot be modified after being initialized
Found 1529 images belonging to 6 classes.
Found 380 images belonging to 6 classes.
Epoch 1/10
24/24 [==============================] - 21s 851ms/step - loss: 1.1108 - accuracy: 0.5134 - val_loss: 0.8370 - val_accuracy: 0.6842
Epoch 2/10
24/24 [==============================] - 21s 862ms/step - loss: 0.6156 - accuracy: 0.7613 - val_loss: 0.4063 - val_accuracy: 0.8447
Epoch 3/10
24/24 [==============================] - 22s 915ms/step - loss: 0.2704 - accuracy: 0.9006 - val_loss: 0.2283 - val_accuracy: 0.9316
Epoch 4/10
24/24 [==============================] - 22s 909ms/step - loss: 0.1532 - accuracy: 0.9477 - val_loss: 0.1145 - val_accuracy: 0.9579
Epoch 5/10
24/24 [==============================] - 22s 905ms/step - loss: 0.1324 - accuracy: 0.9516 - val_loss: 0.2525 - val_accuracy: 0.9026
Epoch 6/10
24/24 [==============================] - 22s 912ms/step - loss: 0.0737 - accuracy: 0.9719 - val_loss: 0.0519 - val_accuracy: 0.9816
Epoch 7/10
24/24 [==============================] - 23s 952ms/step - loss: 0.0663 - accuracy: 0.9797 - val_loss: 0.0544 - val_accuracy: 0.9842
Epoch 8/10
24/24 [==============================] - 22s 918ms/step - loss: 0.0482 - accuracy: 0.9817 - val_loss: 0.0489 - val_accuracy: 0.9711
Epoch 9/10
24/24 [==============================] - 22s 928ms/step - loss: 0.0240 - accuracy: 0.9908 - val_loss: 0.0337 - val_accuracy: 0.9868
Epoch 10/10
24/24 [==============================] - 22s 911ms/step - loss: 0.0224 - accuracy: 0.9922 - val_loss: 0.0411 - val_accuracy: 0.9895
6/6 [==============================] - 4s 717ms/step - loss: 0.0078 - accuracy: 1.0000
Test loss: 0.007844111882150173, Test accuracy: 1.0
Model: "sequential_32"
Epoch 1/50
96/96 [==============================] - 28s 232ms/step - loss: 1.0307 - accuracy: 0.5520 - val_loss: 0.6397 - val_accuracy: 0.7658
Epoch 2/50
96/96 [==============================] - 21s 220ms/step - loss: 0.4214 - accuracy: 0.8457 - val_loss: 0.2777 - val_accuracy: 0.8895
Epoch 3/50
96/96 [==============================] - 21s 222ms/step - loss: 0.1863 - accuracy: 0.9346 - val_loss: 0.2255 - val_accuracy: 0.9105
Epoch 4/50
96/96 [==============================] - 21s 216ms/step - loss: 0.1238 - accuracy: 0.9575 - val_loss: 0.1356 - val_accuracy: 0.9579
Epoch 5/50
96/96 [==============================] - 21s 217ms/step - loss: 0.0868 - accuracy: 0.9647 - val_loss: 0.2071 - val_accuracy: 0.9237
Epoch 6/50
96/96 [==============================] - 21s 217ms/step - loss: 0.0541 - accuracy: 0.9817 - val_loss: 0.4429 - val_accuracy: 0.8974
Epoch 7/50
96/96 [==============================] - 21s 215ms/step - loss: 0.0515 - accuracy: 0.9797 - val_loss: 0.2518 - val_accuracy: 0.9026
Epoch 8/50
96/96 [==============================] - 21s 219ms/step - loss: 0.0409 - accuracy: 0.9869 - val_loss: 0.0942 - val_accuracy: 0.9632
Epoch 9/50
96/96 [==============================] - 20s 210ms/step - loss: 0.0573 - accuracy: 0.9856 - val_loss: 0.2120 - val_accuracy: 0.9421
Epoch 10/50
96/96 [==============================] - 20s 209ms/step - loss: 0.0591 - accuracy: 0.9804 - val_loss: 0.0389 - val_accuracy: 0.9842
Epoch 11/50
96/96 [==============================] - 20s 205ms/step - loss: 0.0145 - accuracy: 0.9948 - val_loss: 0.0340 - val_accuracy: 0.9921
Epoch 12/50
96/96 [==============================] - 20s 205ms/step - loss: 0.0280 - accuracy: 0.9869 - val_loss: 0.2500 - val_accuracy: 0.9263
Epoch 13/50
96/96 [==============================] - 20s 208ms/step - loss: 0.0361 - accuracy: 0.9876 - val_loss: 0.0801 - val_accuracy: 0.9711
Epoch 14/50
96/96 [==============================] - 20s 209ms/step - loss: 0.0388 - accuracy: 0.9856 - val_loss: 0.0241 - val_accuracy: 0.9974
Epoch 15/50
96/96 [==============================] - 20s 207ms/step - loss: 0.0052 - accuracy: 0.9980 - val_loss: 0.0155 - val_accuracy: 0.9947
Epoch 16/50
96/96 [==============================] - 21s 215ms/step - loss: 0.0367 - accuracy: 0.9902 - val_loss: 0.0824 - val_accuracy: 0.9658
Epoch 17/50
96/96 [==============================] - 20s 204ms/step - loss: 0.0461 - accuracy: 0.9830 - val_loss: 0.1926 - val_accuracy: 0.9553
Epoch 18/50
96/96 [==============================] - 21s 214ms/step - loss: 0.0307 - accuracy: 0.9908 - val_loss: 0.0641 - val_accuracy: 0.9789
Epoch 19/50
96/96 [==============================] - 20s 209ms/step - loss: 0.0017 - accuracy: 1.0000 - val_loss: 0.0305 - val_accuracy: 0.9947
Epoch 20/50
96/96 [==============================] - 20s 208ms/step - loss: 0.0060 - accuracy: 0.9980 - val_loss: 0.0251 - val_accuracy: 0.9895
24/24 [==============================] - 4s 167ms/step - loss: 0.0236 - accuracy: 0.9868
Test loss: 0.02361617051064968, Test accuracy: 0.9868420958518982
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_128 (Conv2D) (None, 222, 222, 32) 896
conv2d (Conv2D) (None, 222, 222, 32) 896
max_pooling2d_128 (MaxPooli (None, 111, 111, 32) 0
ng2D)
max_pooling2d (MaxPooling2D (None, 111, 111, 32) 0
)
conv2d_129 (Conv2D) (None, 109, 109, 64) 18496
conv2d_1 (Conv2D) (None, 109, 109, 64) 18496
max_pooling2d_129 (MaxPooli (None, 54, 54, 64) 0
ng2D)
max_pooling2d_1 (MaxPooling (None, 54, 54, 64) 0
2D)
conv2d_130 (Conv2D) (None, 52, 52, 128) 73856
conv2d_2 (Conv2D) (None, 52, 52, 128) 73856
max_pooling2d_130 (MaxPooli (None, 26, 26, 128) 0
ng2D)
max_pooling2d_2 (MaxPooling (None, 26, 26, 128) 0
2D)
conv2d_131 (Conv2D) (None, 24, 24, 256) 295168
conv2d_3 (Conv2D) (None, 24, 24, 256) 295168
max_pooling2d_131 (MaxPooli (None, 12, 12, 256) 0
ng2D)
max_pooling2d_3 (MaxPooling (None, 12, 12, 256) 0
2D)
flatten_32 (Flatten) (None, 36864) 0
flatten (Flatten) (None, 36864) 0
dense_96 (Dense) (None, 256) 9437440
dense (Dense) (None, 256) 9437440
dense_97 (Dense) (None, 128) 32896
dense_1 (Dense) (None, 128) 32896
dense_98 (Dense) (None, 6) 774
dense_2 (Dense) (None, 6) 774
=================================================================
Total params: 9,859,526
Trainable params: 9,859,526
Non-trainable params: 0
_________________________________________________________________
It took: 3.74 minutes
It took: 7.04 minutes
%% Cell type:code id:ec102c63-b7d9-4f22-9f3e-a6bd12995a4f tags:
``` python
```
%% Cell type:code id:17105ae9-d877-488e-ad4d-aac7a9a15680 tags:
``` python
```
......
......@@ -37,7 +37,7 @@ train_set = datagen.flow_from_directory(
test_set = datagen.flow_from_directory(
path,
target_size=(300, 300),
target_size=(224, 224),
batch_size=32,
class_mode='categorical',
subset='validation'
......@@ -45,7 +45,7 @@ test_set = datagen.flow_from_directory(
# Step 4: Build the Model
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(300, 300, 3), activation='relu'))
model.add(Conv2D(32, (3, 3), input_shape=(224, 224, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment