I guess the issue is not with the python specifically but with overall RAM management in Linux. So here's the code:
!pip install numpy opencv-python pandas matplotlib tensorflow scikit-learn
import numpy as np
import cv2 as cv
import pandas as pd
import matplotlib.pyplot as plt
import os
import gc
gc.enable()
train_dir = 'fruits-360/Training'
classes = os.listdir(train_dir)
classes = classes[:30]
all_arrays=[]
img_size=100
for i in classes:
path=os.path.join(train_dir, i)
class_num=classes.index(i)
for img in os.listdir(path):
img_array=cv.imread(os.path.join(path, img))
mig_array=cv.cvtColor(img_array, cv.COLOR_BGR2RGB)
all_arrays.append([img_array, class_num])
test_dir = 'fruits-360/Test'
classes2 = os.listdir(test_dir)
classes2 = classes2[:30]
all_arrays2=[]
img_size=100
for i in classes2:
path=os.path.join(test_dir, i)
class_num2=classes.index(i)
for img in os.listdir(path):
img_array=cv.imread(os.path.join(path, img))
mig_array=cv.cvtColor(img_array, cv.COLOR_BGR2RGB)
all_arrays.append([img_array, class_num2])
import random
random.shuffle(all_arrays)
X_train=[]
Y_train=[]
for features, label in all_arrays:
X_train.append(features)
Y_train.append(label)
X_train=np.array(X_train)
random.shuffle(all_arrays2)
X_test=[]
Y_test=[]
for features, label in all_arrays:
X_test.append(features)
Y_test.append(label)
X_test=np.array(X_test)
X_train=X_train.reshape(-1, img_size, img_size, 3)
X_train=X_train/255
X_test=X_test.reshape(-1, img_size, img_size, 3)
X_test=X_test/255
from keras.utils import to_categorical
Y_train=to_categorical(Y_train, num_classes=30)
Y_test=to_categorical(Y_test, num_classes=30)
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten, MaxPool2D, Dropout
from keras.callbacks import ReduceLROnPlateau
from keras.optimizers import Adam
x_train, x_val, y_train, y_val = train_test_split(X_train, Y_train, test_size=0.3, random_state=42)
model=Sequential()
model.add(Conv2D(filters=16, kernel_size=(5,5), padding='Same', activation='relu', input_shape=(100, 100, 3)))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Conv2D(filters=32, kernel_size=(5,5), padding='Same', activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Conv2D(filters=64, kernel_size=(5,5), padding='Same', activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.6))
model.add(Dense(30, activation='softmax'))
model.compile(optimizer=Adam(learning_rate=0.001),
loss='categorical_crossentropy', metrics=['accuracy'])
epochs=10
batch_size = 32
history=model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs)
y_pred = model.predict(x_train)
y_pred_classes = np.argmax(y_pred, axis=1)
y_true = np.argmax(y_train, axis=1)
conf_mat = confusion_matrix(y_true, y_pred_classes)
disp = ConfusionMatrixDisplay(conf_mat, display_labels=classes)
fig, ax = plt.subplots(figsize=(15,15))
disp.plot(ax=ax)
plt.xticks(rotation=90)
plt.show()
model.summary()
The issue is not with the code itself, provided it just in case. When i run this code on jupyter notebook on windows, overall RAM usage is at about 9/16GB and the code runs fine, however if i run the code on Linux, it consumes all available RAM and swap partition, then jupyter crashes. If i run jupyter notebook with command:
systemd-run --scope -p MemoryMax=8192M jupyter-notebook
Jupyter still crashes after reaching 8 gigs and using the entire swap.
Is there a way to fix it somehow?