Instructions to use OneclickAI/CNN_test_Model with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use OneclickAI/CNN_test_Model with Keras:
# Available backend options are: "jax", "torch", "tensorflow". import os os.environ["KERAS_BACKEND"] = "jax" import keras model = keras.saving.load_model("hf://OneclickAI/CNN_test_Model") - Notebooks
- Google Colab
- Kaggle
| import tensorflow as tf | |
| # hf_hub_download ํจ์๋ฅผ ์ง์ ์ฌ์ฉํ๊ธฐ ์ํด import ํฉ๋๋ค. | |
| from huggingface_hub import hf_hub_download | |
| from PIL import Image, ImageOps | |
| import numpy as np | |
| import os | |
| def load_model_from_hf(model_id, model_filename="model.keras"): | |
| """ | |
| Hugging Face Hub์์ Keras ๋ชจ๋ธ ํ์ผ์ ๋ค์ด๋ก๋ํ ํ, | |
| ๋ก์ปฌ์ ์ ์ฅ๋ ํ์ผ์ ์ด์ฉํด ๋ชจ๋ธ์ ๋ถ๋ฌ์ต๋๋ค. | |
| """ | |
| try: | |
| print(f"Downloading model '{model_id}' from Hugging Face Hub...") | |
| # 1. Hugging Face Hub์์ ๋ชจ๋ธ ํ์ผ ๋ค์ด๋ก๋ | |
| # ์ด ํจ์๋ ๋ค์ด๋ก๋๋ ํ์ผ์ด ์ ์ฅ๋ ๋ก์ปฌ ๊ฒฝ๋ก๋ฅผ ๋ฐํํฉ๋๋ค. | |
| model_path = hf_hub_download(repo_id=model_id, filename=model_filename) | |
| print(f"Model downloaded to: {model_path}") | |
| # 2. ๋ก์ปฌ์ ์ ์ฅ๋ ๋ชจ๋ธ ํ์ผ ๋ก๋ | |
| print("Loading model from local file...") | |
| model = tf.keras.models.load_model(model_path) | |
| print("Model loaded successfully!") | |
| return model | |
| except Exception as e: | |
| print(f"Error loading model: {e}") | |
| print("Please check if the model ID and filename are correct on Hugging Face Hub.") | |
| return None | |
| def preprocess_image(image_path): | |
| """ | |
| ์ฌ์ฉ์ ์ด๋ฏธ์ง๋ฅผ MNIST ๋ฐ์ดํฐ์ ํ์์ ๋ง๊ฒ ์ ์ฒ๋ฆฌํฉ๋๋ค. | |
| """ | |
| try: | |
| # 1. ์ด๋ฏธ์ง ์ด๊ธฐ | |
| img = Image.open(image_path) | |
| # 2. ํ๋ฐฑ(Grayscale)์ผ๋ก ๋ณํ | |
| img = img.convert('L') | |
| # 3. ์์ ๋ฐ์ (MNIST๋ ํฐ์ ๊ธ์จ/๊ฒ์ ๋ฐฐ๊ฒฝ, ์ฌ์ฉ์๋ ๋ณดํต ๊ฒ์ ๊ธ์จ/ํฐ์ ๋ฐฐ๊ฒฝ) | |
| if np.mean(np.array(img)) > 128: | |
| img = ImageOps.invert(img) | |
| # 4. 28x28 ํฌ๊ธฐ๋ก ๋ฆฌ์ฌ์ด์ฆ | |
| img = img.resize((28, 28), Image.Resampling.LANCZOS) | |
| # 5. Numpy ๋ฐฐ์ด๋ก ๋ณํํ๊ณ 0~1 ์ฌ์ด ๊ฐ์ผ๋ก ์ ๊ทํ | |
| img_array = np.array(img).astype('float32') / 255.0 | |
| # 6. ๋ชจ๋ธ์ ์ ๋ ฅ ํํ์ ๋ง๊ฒ ์ฐจ์ ํ์ฅ (1, 28, 28, 1) | |
| processed_img = np.expand_dims(img_array, axis=0) | |
| processed_img = np.expand_dims(processed_img, axis=-1) | |
| return processed_img | |
| except FileNotFoundError: | |
| print(f"Error: The file '{image_path}' was not found.") | |
| return None | |
| except Exception as e: | |
| print(f"Error processing image: {e}") | |
| return None | |
| def main(): | |
| # Hugging Face์ ์ ๋ก๋๋ ๋ชจ๋ธ ID | |
| model_id = "OneclickAI/CNN_test_Model" | |
| # ๋ชจ๋ธ ๋ก๋ (์์ ๋ ํจ์ ํธ์ถ) | |
| # ์ด์ train.py์์ model.save("my_keras_model.keras")๋ก ์ ์ฅํ์ผ๋ฏ๋ก, | |
| # Hub์ ์ฌ๋ผ๊ฐ ํ์ผ ์ด๋ฆ์ 'my_keras_model.keras'์ผ ๊ฒ์ ๋๋ค. | |
| # ๋ง์ฝ ๋ค๋ฅธ ์ด๋ฆ์ผ๋ก ์ฌ๋ ธ๋ค๋ฉด ํด๋น ํ์ผ๋ช ์ผ๋ก ์์ ํด์ฃผ์ธ์. | |
| # (Hugging Face Hub์์๋ ๋ณดํต 'model.keras' ๋ผ๋ ์ด๋ฆ์ ๊ถ์ฅํฉ๋๋ค) | |
| model = load_model_from_hf(model_id, model_filename="my_keras_model.keras") | |
| if model is None: | |
| return # ๋ชจ๋ธ ๋ก๋ ์คํจ ์ ์ข ๋ฃ | |
| # ์ฌ์ฉ์์๊ฒ ์ด๋ฏธ์ง ๊ฒฝ๋ก๋ฅผ ๊ณ์ํด์ ์ ๋ ฅ๋ฐ์ | |
| while True: | |
| user_input = input("\nPlease enter the path to your image (or type 'exit' to quit): ") | |
| if user_input.lower() == 'exit': | |
| break | |
| if not os.path.exists(user_input): | |
| print(f"File not found at '{user_input}'. Please check the path and try again.") | |
| continue | |
| # ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ | |
| processed_image = preprocess_image(user_input) | |
| if processed_image is not None: | |
| # ๋ชจ๋ธ ์์ธก ์ํ | |
| predictions = model.predict(processed_image) | |
| # ๊ฐ์ฅ ๋์ ํ๋ฅ ์ ๊ฐ์ง ํด๋์ค(์ซ์)๋ฅผ ์ฐพ์ | |
| predicted_digit = np.argmax(predictions[0]) | |
| confidence = np.max(predictions[0]) * 100 | |
| print("\n--- Prediction Result ---") | |
| print(f"Predicted Digit: {predicted_digit}") | |
| print(f"Confidence: {confidence:.2f}%") | |
| print("-------------------------") | |
| if __name__ == "__main__": | |
| main() |