Deep Learning for Image Classification and Stock Prediction

Deep Learning Models for Image and Time Series Analysis

This document demonstrates the application of two prominent deep learning architectures: Convolutional Neural Networks (CNNs) for image classification and Long Short-Term Memory (LSTM) networks for time series forecasting. Each section provides a step-by-step implementation, from data preparation to model evaluation and prediction.

Convolutional Neural Networks for Image Classification

This section details the process of building and training a CNN for image classification, specifically using a brain tumor MRI dataset. We will cover data preparation, model architecture, training, evaluation, and making predictions on new images.

Dataset Preparation

First, we import necessary libraries and extract the dataset from a ZIP archive. The dataset is then prepared using Keras’s ImageDataGenerator for training and testing, including data augmentation for the training set to improve model generalization.

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array
import zipfile, os, numpy as np

with zipfile.ZipFile(r'mridataset.zip', 'r') as zip_ref:
    zip_ref.extractall('./brain_tumor_mri_data')

train_dir = './brain_tumor_mri_data/Training'
test_dir = './brain_tumor_mri_data/Testing'

train_gen = ImageDataGenerator(rescale=1./255, rotation_range=20, width_shift_range=0.2,
                               height_shift_range=0.2, shear_range=0.2, zoom_range=0.2,
                               horizontal_flip=True)
test_gen = ImageDataGenerator(rescale=1./255)

train_data = train_gen.flow_from_directory(train_dir, target_size=(128,128), batch_size=32, class_mode='categorical')
test_data = test_gen.flow_from_directory(test_dir, target_size=(128,128), batch_size=32, class_mode='categorical')

CNN Model Architecture

A sequential CNN model is constructed with multiple convolutional and pooling layers, followed by dense layers for classification. A dropout layer is included to prevent overfitting and improve robustness.

model = Sequential([
    Conv2D(32, (3,3), activation='relu', input_shape=(128,128,3)), MaxPooling2D(2,2),
    Conv2D(64, (3,3), activation='relu'), MaxPooling2D(2,2),
    Conv2D(128, (3,3), activation='relu'), MaxPooling2D(2,2),
    Flatten(), Dense(128, activation='relu'), Dropout(0.5),
    Dense(train_data.num_classes, activation='softmax')
])

Model Compilation and Training

The model is compiled using the Adam optimizer and categorical crossentropy loss, which is suitable for multi-class classification. It is then trained on the prepared image data, with validation performed on the test set.

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(train_data, epochs=20, validation_data=test_data)

Model Evaluation and Prediction

After training, the model’s performance is evaluated on the test dataset to determine its accuracy. We also demonstrate how to load a single image and use the trained model to make a prediction, mapping the predicted class index back to a human-readable label.

loss, acc = model.evaluate(test_data)
print(f"Test Accuracy: {acc*100:.2f}%")

img = load_img(r'sample.jpg', target_size=(128, 128))
img_array = np.expand_dims(img_to_array(img)/255.0, axis=0)
pred = model.predict(img_array)

class_labels = {v: k for k, v in train_data.class_indices.items()}
print("Prediction:", class_labels[np.argmax(pred)])

Long Short-Term Memory Networks for Stock Price Prediction

This section focuses on using Long Short-Term Memory (LSTM) networks, a type of recurrent neural network particularly effective for time series forecasting. We will predict stock prices using historical Apple (AAPL) stock data.

Data Acquisition and Preprocessing

We download historical stock data for Apple (AAPL) from Yahoo Finance. The ‘Close’ prices are then preprocessed using Min-Max scaling to normalize the values between 0 and 1, which is a common practice for neural network inputs.

import numpy as np, pandas as pd, yfinance as yf, matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense

data = yf.download('AAPL', start='2010-01-01', end='2024-01-01')[['Close']].values
scaler = MinMaxScaler((0, 1))
data_scaled = scaler.fit_transform(data)

Sequence Creation for LSTM

LSTMs require input data to be in sequences. A custom function make_seq is defined to transform the time series data into sequences (X) and corresponding target values (Y) for supervised learning. The data is then reshaped and split into training and testing sets.

def make_seq(data, step):
    X, Y = [], []
    for i in range(len(data) - step):
        X.append(data[i:i+step, 0])
        Y.append(data[i+step, 0])
    return np.array(X), np.array(Y)

step = 60
X, Y = make_seq(data_scaled, step)
X = X.reshape(-1, step, 1)

split = int(len(X) * 0.8)
X_train, Y_train = X[:split], Y[:split]
X_test, Y_test = X[split:], Y[split:]

LSTM Model Definition and Training

A sequential LSTM model is built with multiple LSTM layers followed by dense layers. The model is compiled with the Adam optimizer and mean squared error (MSE) loss, which is appropriate for regression tasks. It is then trained on the prepared sequences.

model = Sequential([
    LSTM(50, return_sequences=True, input_shape=(step, 1)),
    LSTM(50), Dense(25), Dense(1)
])
model.compile(optimizer='adam', loss='mse')
model.fit(X_train, Y_train, epochs=20, batch_size=32, validation_data=(X_test, Y_test))

Prediction and Visualization

Finally, the trained LSTM model makes predictions on the test set. The predictions and actual values are inverse-transformed to their original scale, and then plotted using Matplotlib to visually compare the model’s forecasting performance against the true stock prices.

pred = scaler.inverse_transform(model.predict(X_test))
actual = scaler.inverse_transform(Y_test.reshape(-1, 1))

plt.figure(figsize=(12,6))
plt.plot(actual, label='Actual', color='blue')
plt.plot(pred, label='Predicted', color='red')
plt.legend(), plt.xlabel("Time"), plt.ylabel("Stock Price"), plt.show()