This repository contains code for a deep learning model that predicts the phase of the moon from an input image. The model is built using Convolutional Neural Networks (CNNs) and classifies images into one of the eight moon phases.
The goal of this project is to accurately classify images of the moon into one of eight phases:
- New Moon
- Waxing Crescent
- First Quarter
- Waxing Gibbous
- Full Moon
- Waning Gibbous
- Last Quarter
- Waning Crescent
Initially the datatset was taken from kaggle that consisted of images of the moon. I labeled the images with their respective phases, such that, images are organized in subdirectories named after the phases. Ensure that the dataset is unzipped and placed in the correct directory structure.
The model is a Convolutional Neural Network (CNN) with the following architecture:
- Convolutional layers with ReLU activation
- MaxPooling layers
- Flatten layer
- Dense layers with ReLU activation
- Dropout layer
- Output layer with softmax activation
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(IMG_SIZE, IMG_SIZE, 3)),
MaxPooling2D(2, 2),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D(2, 2),
Flatten(),
Dense(128, activation='relu'),
Dropout(0.5),
Dense(len(CATEGORIES), activation='softmax')
])
model.summary()
The images are loaded, resized, and normalized. The labels are converted to categorical format. The dataset is split into training and testing sets. The model is then compiled and trained.
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
history = model.fit(X_train, y_train, epochs=20, validation_data=(X_test, y_test))
The model's performance is evaluated on the test set.
loss, accuracy = model.evaluate(X_test, y_test)
print("Test Accuracy:", accuracy)
You can use the trained model to predict the moon phase of new images.
def predict_moon_phase(model, img_path):
img = cv2.imread(img_path)
img_resized = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
img_normalized = img_resized / 255.0
img_reshaped = np.reshape(img_normalized, (1, IMG_SIZE, IMG_SIZE, 3))
prediction = model.predict(img_reshaped)
return CATEGORIES[np.argmax(prediction)]
Testing model on different images
img_path = 'test.jpg'
predicted_phase = predict_moon_phase(model, img_path)
print("The predicted moon phase is:", predicted_phase)
img_path = 'test1.jpg'
predicted_phase = predict_moon_phase(model, img_path)
print("The predicted moon phase is:", predicted_phase)
img_path = 'test2.jpg'
predicted_phase = predict_moon_phase(model, img_path)
print("The predicted moon phase is:", predicted_phase)
The training and validation loss and accuracy are plotted to visualize the model's performance over epochs.