diff --git a/Dockerfile b/Dockerfile index 9ee9642..a785e19 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,18 +1,13 @@ -FROM python:2-alpine +from sklearn.model_selection import train_test_split -COPY ./requirements.txt /app/requirements.txt +def split_train_dev_test(X, y, test_size, dev_size): + # Split into train+dev and test sets first + X_temp, X_test, y_temp, y_test = train_test_split(X, y, test_size=test_size) -WORKDIR /app + # Compute actual dev size relative to the combined train+dev set + actual_dev_size = dev_size / (1 - test_size) + + # Split the train+dev set into separate training and dev sets + X_train, X_dev, y_train, y_dev = train_test_split(X_temp, y_temp, test_size=actual_dev_size) -RUN apk --update add python py-pip openssl ca-certificates py-openssl wget bash linux-headers -RUN apk --update add --virtual build-dependencies libffi-dev openssl-dev python-dev py-pip build-base \ - && pip install --upgrade pip \ - && pip install --upgrade pipenv\ - && pip install --upgrade -r /app/requirements.txt\ - && apk del build-dependencies - -COPY . /app - -ENTRYPOINT [ "python" ] - -CMD [ "hello.py" ] \ No newline at end of file + return X_train, X_dev, X_test, y_train, y_dev, y_test diff --git a/README.md b/README.md index 45a9b69..9695c8d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # Getting Started with Python on IBM Cloud + To get started, we'll take you through a sample Python Flask app, help you set up a development environment, deploy to IBM Cloud and add a Cloudant database. The following instructions are for deploying the application as a Cloud Foundry application. To deploy as a container to **IBM Cloud Kubernetes Service** instead, [see README-kubernetes.md](README-kubernetes.md) diff --git a/hello.py b/hello.py index 9218339..67546ee 100644 --- a/hello.py +++ b/hello.py @@ -86,3 +86,18 @@ def shutdown(): if __name__ == '__main__': app.run(host='0.0.0.0', port=port, debug=True) + + + + + +# Calculate the total number of samples +total_samples = len(train_data) + len(test_data) + len(dev_data) + +# Get the image dimensions (assuming all images have the same size) +image_height, image_width = train_data[0].shape[:2] # Assuming the shape is (height, width, channels) + +# Print statements +print(f"Total number of samples in the dataset: {total_samples}") +print(f"Size of the images in the dataset: {image_height}x{image_width}") + diff --git a/image_resize.py b/image_resize.py new file mode 100644 index 0000000..9316504 --- /dev/null +++ b/image_resize.py @@ -0,0 +1,19 @@ +import numpy as np +from skimage.transform import resize + +def resize_images(images, size): + """Resizes images to the given size. + + Args: + images: A numpy array of images. + size: The target size of the images. + + Returns: + A numpy array of resized images. + """ + + resized_images = np.zeros((images.shape[0], size[0], size[1], images.shape[3])) + for i in range(images.shape[0]): + resized_images[i] = resize(images[i], size, order=3) + return resized_images + diff --git a/quiz3.py b/quiz3.py new file mode 100644 index 0000000..914958d --- /dev/null +++ b/quiz3.py @@ -0,0 +1,38 @@ +from flask import Flask, request, jsonify +import tensorflow as tf +import numpy as np +from PIL import Image + +app = Flask(__name__) + +@app.route('/predict', methods=['POST']) +def predict(): + # Get the two images from the request + image1 = request.files['image1'] + image2 = request.files['image2'] + + # Convert the images to numpy arrays + image1_array = np.array(Image.open(image1)) + image2_array = np.array(Image.open(image2)) + + # Preprocess the images + image1_array = image1_array / 255.0 + image1_array = image1_array.reshape(1, 28, 28, 1) + image2_array = image2_array / 255.0 + image2_array = image2_array.reshape(1, 28, 28, 1) + + # Load the trained model + model = tf.keras.models.load_model('model.h5') + + # Make predictions on the images + prediction1 = model.predict(image1_array) + prediction2 = model.predict(image2_array) + + # Check if the predictions are the same + if np.argmax(prediction1) == np.argmax(prediction2): + return jsonify({'is_same_digit': True}) + else: + return jsonify({'is_same_digit': False}) + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=5000)