maanbumigu 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- maanbumigu-0.1.0/PKG-INFO +49 -0
- maanbumigu-0.1.0/README.md +37 -0
- maanbumigu-0.1.0/pyproject.toml +23 -0
- maanbumigu-0.1.0/setup.cfg +4 -0
- maanbumigu-0.1.0/src/maanbumigu/__init__.py +120 -0
- maanbumigu-0.1.0/src/maanbumigu/snippets/__init__.py +0 -0
- maanbumigu-0.1.0/src/maanbumigu/snippets/cnn.py +87 -0
- maanbumigu-0.1.0/src/maanbumigu/snippets/dnn.py +88 -0
- maanbumigu-0.1.0/src/maanbumigu/snippets/fcnn.py +91 -0
- maanbumigu-0.1.0/src/maanbumigu/snippets/frcnn_resnet.py +89 -0
- maanbumigu-0.1.0/src/maanbumigu/snippets/rnn_lstm.py +161 -0
- maanbumigu-0.1.0/src/maanbumigu.egg-info/PKG-INFO +49 -0
- maanbumigu-0.1.0/src/maanbumigu.egg-info/SOURCES.txt +13 -0
- maanbumigu-0.1.0/src/maanbumigu.egg-info/dependency_links.txt +1 -0
- maanbumigu-0.1.0/src/maanbumigu.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: maanbumigu
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Personal code snippet library - retrieve saved source code via mb.get()
|
|
5
|
+
Author: arunkumar
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: snippets,tensorflow,pytorch,dnn,cnn
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Requires-Python: >=3.9
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# maanbumigu
|
|
14
|
+
|
|
15
|
+
Personal snippet library. Keeps your source code as-is and retrieves it with one line:
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
import maanbumigu as mb
|
|
19
|
+
|
|
20
|
+
mb.list() # ['CNN', 'DNN', 'FCNN', 'FRCNN_RESNET', 'RNN_LSTM']
|
|
21
|
+
mb.get("DNN") # prints + returns DNN code
|
|
22
|
+
mb.get("CNN")
|
|
23
|
+
mb.get("FCNN")
|
|
24
|
+
mb.get("FRCNN") # alias for FRCNN_RESNET
|
|
25
|
+
mb.get("RNN") # alias for RNN_LSTM
|
|
26
|
+
mb.get("LSTM") # alias for RNN_LSTM
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Save to file:
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
mb.get("DNN", save_as="dnn_run.py")
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Publish to PyPI (maintainer)
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
cd /Users/arunkumar/maanbumigu
|
|
39
|
+
python3 -m pip install --upgrade build twine
|
|
40
|
+
python3 -m build
|
|
41
|
+
python3 -m twine upload dist/*
|
|
42
|
+
# enter your PyPI username + password / API token when asked
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Then anyone can install:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
pip install maanbumigu
|
|
49
|
+
```
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# maanbumigu
|
|
2
|
+
|
|
3
|
+
Personal snippet library. Keeps your source code as-is and retrieves it with one line:
|
|
4
|
+
|
|
5
|
+
```python
|
|
6
|
+
import maanbumigu as mb
|
|
7
|
+
|
|
8
|
+
mb.list() # ['CNN', 'DNN', 'FCNN', 'FRCNN_RESNET', 'RNN_LSTM']
|
|
9
|
+
mb.get("DNN") # prints + returns DNN code
|
|
10
|
+
mb.get("CNN")
|
|
11
|
+
mb.get("FCNN")
|
|
12
|
+
mb.get("FRCNN") # alias for FRCNN_RESNET
|
|
13
|
+
mb.get("RNN") # alias for RNN_LSTM
|
|
14
|
+
mb.get("LSTM") # alias for RNN_LSTM
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Save to file:
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
mb.get("DNN", save_as="dnn_run.py")
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Publish to PyPI (maintainer)
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
cd /Users/arunkumar/maanbumigu
|
|
27
|
+
python3 -m pip install --upgrade build twine
|
|
28
|
+
python3 -m build
|
|
29
|
+
python3 -m twine upload dist/*
|
|
30
|
+
# enter your PyPI username + password / API token when asked
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Then anyone can install:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install maanbumigu
|
|
37
|
+
```
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "maanbumigu"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Personal code snippet library - retrieve saved source code via mb.get()"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "arunkumar" }]
|
|
13
|
+
keywords = ["snippets", "tensorflow", "pytorch", "dnn", "cnn"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"License :: OSI Approved :: MIT License",
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
[tool.setuptools.packages.find]
|
|
20
|
+
where = ["src"]
|
|
21
|
+
|
|
22
|
+
[tool.setuptools.package-data]
|
|
23
|
+
maanbumigu = ["snippets/*.py"]
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"""maanbumigu - personal code snippet library.
|
|
2
|
+
|
|
3
|
+
Usage:
|
|
4
|
+
import maanbumigu as mb
|
|
5
|
+
mb.list() # see all available keys
|
|
6
|
+
code = mb.get("DNN") # returns + prints source code as-is
|
|
7
|
+
mb.get("CNN", save_as="cnn_run.py") # also save to file
|
|
8
|
+
"""
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import pathlib
|
|
12
|
+
|
|
13
|
+
__version__ = "0.1.0"
|
|
14
|
+
|
|
15
|
+
_SNIPPET_DIR = pathlib.Path(__file__).parent / "snippets"
|
|
16
|
+
|
|
17
|
+
# canonical key -> snippet filename
|
|
18
|
+
_SNIPPETS = {
|
|
19
|
+
"DNN": "dnn.py",
|
|
20
|
+
"CNN": "cnn.py",
|
|
21
|
+
"FCNN": "fcnn.py",
|
|
22
|
+
"FRCNN_RESNET": "frcnn_resnet.py",
|
|
23
|
+
"RNN_LSTM": "rnn_lstm.py",
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
# aliases (case-insensitive) -> canonical key
|
|
27
|
+
_ALIASES = {
|
|
28
|
+
"DNN": "DNN",
|
|
29
|
+
"DNN_TENSORFLOW": "DNN",
|
|
30
|
+
"DNN_CLASSIFICATION": "DNN",
|
|
31
|
+
"CNN": "CNN",
|
|
32
|
+
"CNN_CLASSIFICATION": "CNN",
|
|
33
|
+
"FCNN": "FCNN",
|
|
34
|
+
"FCNN_SEGMENTATION": "FCNN",
|
|
35
|
+
"FCN": "FCNN",
|
|
36
|
+
"FRCNN": "FRCNN_RESNET",
|
|
37
|
+
"FRCNN_RESNET": "FRCNN_RESNET",
|
|
38
|
+
"FASTER_RCNN": "FRCNN_RESNET",
|
|
39
|
+
"FASTER_RCNN_RESNET": "FRCNN_RESNET",
|
|
40
|
+
"FASTER_RCNN_RESNET50": "FRCNN_RESNET",
|
|
41
|
+
"RESNET": "FRCNN_RESNET",
|
|
42
|
+
"EX3": "FRCNN_RESNET",
|
|
43
|
+
"RNN": "RNN_LSTM",
|
|
44
|
+
"LSTM": "RNN_LSTM",
|
|
45
|
+
"RNN_LSTM": "RNN_LSTM",
|
|
46
|
+
"RNN-LSTM": "RNN_LSTM",
|
|
47
|
+
"TIMESERIES": "RNN_LSTM",
|
|
48
|
+
"TIME_SERIES": "RNN_LSTM",
|
|
49
|
+
"EX10": "RNN_LSTM",
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _resolve(name: str) -> str:
|
|
54
|
+
key = str(name).strip().upper().replace(" ", "_").replace("-", "_")
|
|
55
|
+
if key in _ALIASES:
|
|
56
|
+
return _ALIASES[key]
|
|
57
|
+
# also allow direct filename without extension
|
|
58
|
+
for canon, fname in _SNIPPETS.items():
|
|
59
|
+
if key == fname.upper().replace(".PY", ""):
|
|
60
|
+
return canon
|
|
61
|
+
valid = sorted(set(list(_SNIPPETS) + list(_ALIASES)))
|
|
62
|
+
raise KeyError(f"Unknown snippet {name!r}. Available: {valid}")
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def list(): # noqa: A001 - keep simple API mb.list()
|
|
66
|
+
"""Return list of canonical snippet keys."""
|
|
67
|
+
return sorted(_SNIPPETS)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
keys = list
|
|
71
|
+
available = list
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def describe() -> dict:
|
|
75
|
+
"""Return {canonical_key: description}."""
|
|
76
|
+
return {
|
|
77
|
+
"DNN": "DNN for image classification - Tensorflow/Keras (from .docx, Dense 512/256, as-is)",
|
|
78
|
+
"CNN": "CNN for image classification - Tensorflow/Keras (Conv2D 32/64, as-is)",
|
|
79
|
+
"FCNN": "FCNN image segmentation - encoder/decoder with Conv2DTranspose (as-is)",
|
|
80
|
+
"FRCNN_RESNET": "Faster R-CNN ResNet50 FPN object detection - torch/torchvision (EX_3 notebook, as-is)",
|
|
81
|
+
"RNN_LSTM": "RNN + LSTM time-series forecasting - DOM hourly power (Ex No 10 notebook, as-is)",
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def get(name: str, print_code: bool = True, save_as: str | None = None) -> str:
|
|
86
|
+
"""Retrieve snippet source code as-is.
|
|
87
|
+
|
|
88
|
+
Example:
|
|
89
|
+
import maanbumigu as mb
|
|
90
|
+
mb.get("DNN")
|
|
91
|
+
mb.get("cnn", save_as="cnn_run.py")
|
|
92
|
+
|
|
93
|
+
Args:
|
|
94
|
+
name: e.g. "DNN", "CNN", "FCNN", "FRCNN", "FRCNN_RESNET", "RNN", "LSTM".
|
|
95
|
+
Case-insensitive. Use mb.list() to see canonical keys.
|
|
96
|
+
print_code: if True, print the code to stdout.
|
|
97
|
+
save_as: optional filepath to also write the code to.
|
|
98
|
+
|
|
99
|
+
Returns:
|
|
100
|
+
The source code string (unchanged).
|
|
101
|
+
"""
|
|
102
|
+
canon = _resolve(name)
|
|
103
|
+
path = _SNIPPET_DIR / _SNIPPETS[canon]
|
|
104
|
+
code = path.read_text(encoding="utf-8")
|
|
105
|
+
if print_code:
|
|
106
|
+
print(f"# --- maanbumigu[{canon}] from {path.name} ---")
|
|
107
|
+
print(code)
|
|
108
|
+
if save_as:
|
|
109
|
+
out = pathlib.Path(save_as)
|
|
110
|
+
out.write_text(code, encoding="utf-8")
|
|
111
|
+
print(f"# saved to {out.resolve()}")
|
|
112
|
+
return code
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def show(name: str) -> str:
|
|
116
|
+
"""Alias for get(name)."""
|
|
117
|
+
return get(name)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
__all__ = ["get", "show", "list", "keys", "available", "describe", "__version__"]
|
|
File without changes
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import tensorflow as tf
|
|
2
|
+
from tensorflow.keras.models import Sequential
|
|
3
|
+
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
|
|
4
|
+
from tensorflow.keras.utils import to_categorical
|
|
5
|
+
from sklearn.model_selection import train_test_split
|
|
6
|
+
from sklearn.metrics import classification_report, confusion_matrix
|
|
7
|
+
import numpy as np
|
|
8
|
+
import os
|
|
9
|
+
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
|
10
|
+
import matplotlib.pyplot as plt
|
|
11
|
+
|
|
12
|
+
# Load and preprocess images from directories
|
|
13
|
+
def load_images_from_folder(folder_path, image_size=(128, 128)):
|
|
14
|
+
images = []
|
|
15
|
+
labels = []
|
|
16
|
+
class_names = os.listdir(folder_path)
|
|
17
|
+
for label, class_name in enumerate(class_names):
|
|
18
|
+
class_path = os.path.join(folder_path, class_name)
|
|
19
|
+
for filename in os.listdir(class_path):
|
|
20
|
+
img = load_img(os.path.join(class_path, filename), target_size=image_size)
|
|
21
|
+
img_array = img_to_array(img) / 255.0 # Normalize
|
|
22
|
+
images.append(img_array)
|
|
23
|
+
labels.append(label)
|
|
24
|
+
return np.array(images), np.array(labels), class_names
|
|
25
|
+
|
|
26
|
+
# Path to your dataset folder (structured as folder/class_name/image.jpg)
|
|
27
|
+
dataset_path = '<filepath>'
|
|
28
|
+
|
|
29
|
+
# Load data
|
|
30
|
+
X, y, class_names = load_images_from_folder(dataset_path)
|
|
31
|
+
print(f"Loaded {X.shape[0]} images with shape {X.shape[1:]}.")
|
|
32
|
+
|
|
33
|
+
# Train-test split
|
|
34
|
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
|
35
|
+
|
|
36
|
+
# Convert labels to categorical (one-hot encoding)
|
|
37
|
+
num_classes = len(class_names)
|
|
38
|
+
y_train = to_categorical(y_train, num_classes)
|
|
39
|
+
y_test = to_categorical(y_test, num_classes)
|
|
40
|
+
|
|
41
|
+
# Define a simple CNN model
|
|
42
|
+
model = Sequential([
|
|
43
|
+
Conv2D(32, (3, 3), activation='relu', input_shape=(128, 128, 3)),
|
|
44
|
+
MaxPooling2D((2, 2)),
|
|
45
|
+
Conv2D(64, (3, 3), activation='relu'),
|
|
46
|
+
MaxPooling2D((2, 2)),
|
|
47
|
+
Flatten(),
|
|
48
|
+
Dense(128, activation='relu'),
|
|
49
|
+
Dropout(0.5),
|
|
50
|
+
Dense(num_classes, activation='softmax')
|
|
51
|
+
])
|
|
52
|
+
|
|
53
|
+
# Compile the model
|
|
54
|
+
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
|
|
55
|
+
|
|
56
|
+
# Train the model
|
|
57
|
+
history = model.fit(X_train, y_train, epochs=10, validation_split=0.2, batch_size=32)
|
|
58
|
+
|
|
59
|
+
# Evaluate the model
|
|
60
|
+
test_loss, test_accuracy = model.evaluate(X_test, y_test)
|
|
61
|
+
print(f"Test Accuracy: {test_accuracy * 100:.2f}%")
|
|
62
|
+
|
|
63
|
+
# Predictions and performance metrics
|
|
64
|
+
y_pred = np.argmax(model.predict(X_test), axis=-1)
|
|
65
|
+
y_true = np.argmax(y_test, axis=-1)
|
|
66
|
+
print("Classification Report:")
|
|
67
|
+
print(classification_report(y_true, y_pred, target_names=class_names))
|
|
68
|
+
print("Confusion Matrix:")
|
|
69
|
+
print(confusion_matrix(y_true, y_pred))
|
|
70
|
+
|
|
71
|
+
# Plot training history
|
|
72
|
+
def plot_history(history):
|
|
73
|
+
plt.figure(figsize=(12, 4))
|
|
74
|
+
plt.subplot(1, 2, 1)
|
|
75
|
+
plt.plot(history.history['accuracy'], label='Train Accuracy')
|
|
76
|
+
plt.plot(history.history['val_accuracy'], label='Val Accuracy')
|
|
77
|
+
plt.title('Accuracy')
|
|
78
|
+
plt.legend()
|
|
79
|
+
|
|
80
|
+
plt.subplot(1, 2, 2)
|
|
81
|
+
plt.plot(history.history['loss'], label='Train Loss')
|
|
82
|
+
plt.plot(history.history['val_loss'], label='Val Loss')
|
|
83
|
+
plt.title('Loss')
|
|
84
|
+
plt.legend()
|
|
85
|
+
plt.show()
|
|
86
|
+
|
|
87
|
+
plot_history(history)
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import os
|
|
3
|
+
from sklearn.model_selection import train_test_split
|
|
4
|
+
from sklearn.metrics import classification_report, confusion_matrix
|
|
5
|
+
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
|
6
|
+
from tensorflow.keras.models import Sequential
|
|
7
|
+
from tensorflow.keras.layers import Dense, Flatten, Dropout
|
|
8
|
+
from tensorflow.keras.utils import to_categorical
|
|
9
|
+
import matplotlib.pyplot as plt
|
|
10
|
+
|
|
11
|
+
# Function to load images and their labels from the dataset folder
|
|
12
|
+
def load_images_from_folder(folder_path, image_size=(128, 128)):
|
|
13
|
+
images = []
|
|
14
|
+
labels = []
|
|
15
|
+
class_names = os.listdir(folder_path)
|
|
16
|
+
for label, class_name in enumerate(class_names):
|
|
17
|
+
class_path = os.path.join(folder_path, class_name)
|
|
18
|
+
for filename in os.listdir(class_path):
|
|
19
|
+
img = load_img(os.path.join(class_path, filename), target_size=image_size)
|
|
20
|
+
img_array = img_to_array(img) / 255.0 # Normalize pixel values
|
|
21
|
+
images.append(img_array)
|
|
22
|
+
labels.append(label)
|
|
23
|
+
return np.array(images), np.array(labels), class_names
|
|
24
|
+
|
|
25
|
+
# Load the dataset
|
|
26
|
+
dataset_path = '<filepath>'
|
|
27
|
+
X, y, class_names = load_images_from_folder(dataset_path)
|
|
28
|
+
print(f"Loaded {X.shape[0]} images of size {X.shape[1:]}.")
|
|
29
|
+
|
|
30
|
+
# Train-test split
|
|
31
|
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
|
32
|
+
|
|
33
|
+
# Flatten the image data to feed into the DNN
|
|
34
|
+
X_train_flat = X_train.reshape(X_train.shape[0], -1)
|
|
35
|
+
X_test_flat = X_test.reshape(X_test.shape[0], -1)
|
|
36
|
+
|
|
37
|
+
# Convert labels to one-hot encoding
|
|
38
|
+
num_classes = len(class_names)
|
|
39
|
+
y_train = to_categorical(y_train, num_classes)
|
|
40
|
+
y_test = to_categorical(y_test, num_classes)
|
|
41
|
+
|
|
42
|
+
# Build the DNN model
|
|
43
|
+
model = Sequential([
|
|
44
|
+
Dense(512, activation='relu', input_shape=(X_train_flat.shape[1],)),
|
|
45
|
+
Dense(256, activation='relu'),
|
|
46
|
+
Dense(num_classes, activation='softmax')
|
|
47
|
+
])
|
|
48
|
+
|
|
49
|
+
# Compile the model
|
|
50
|
+
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
|
|
51
|
+
|
|
52
|
+
# Train the model
|
|
53
|
+
history = model.fit(X_train_flat, y_train, epochs=10, validation_split=0.2, batch_size=32)
|
|
54
|
+
|
|
55
|
+
# Evaluate the model
|
|
56
|
+
test_loss, test_accuracy = model.evaluate(X_test_flat, y_test)
|
|
57
|
+
print(f"Test Accuracy: {test_accuracy * 100:.2f}%")
|
|
58
|
+
|
|
59
|
+
# Generate predictions and evaluate the model
|
|
60
|
+
y_pred = np.argmax(model.predict(X_test_flat), axis=-1)
|
|
61
|
+
y_true = np.argmax(y_test, axis=-1)
|
|
62
|
+
|
|
63
|
+
# Display the classification report and confusion matrix
|
|
64
|
+
print("Classification Report:")
|
|
65
|
+
print(classification_report(y_true, y_pred, target_names=class_names))
|
|
66
|
+
print("Confusion Matrix:")
|
|
67
|
+
print(confusion_matrix(y_true, y_pred))
|
|
68
|
+
|
|
69
|
+
# Plot the training history
|
|
70
|
+
def plot_history(history):
|
|
71
|
+
plt.figure(figsize=(12, 4))
|
|
72
|
+
|
|
73
|
+
# Plot accuracy
|
|
74
|
+
plt.subplot(1, 2, 1)
|
|
75
|
+
plt.plot(history.history['accuracy'], label='Train Accuracy')
|
|
76
|
+
plt.plot(history.history['val_accuracy'], label='Val Accuracy')
|
|
77
|
+
plt.title('Model Accuracy')
|
|
78
|
+
plt.legend()
|
|
79
|
+
|
|
80
|
+
# Plot loss
|
|
81
|
+
plt.subplot(1, 2, 2)
|
|
82
|
+
plt.plot(history.history['loss'], label='Train Loss')
|
|
83
|
+
plt.plot(history.history['val_loss'], label='Val Loss')
|
|
84
|
+
plt.title('Model Loss')
|
|
85
|
+
plt.legend()
|
|
86
|
+
plt.show()
|
|
87
|
+
|
|
88
|
+
plot_history(history)
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import tensorflow as tf
|
|
2
|
+
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
|
3
|
+
|
|
4
|
+
# Paths to images and masks directories
|
|
5
|
+
image_dir = "image path"
|
|
6
|
+
mask_dir = "mask path"
|
|
7
|
+
|
|
8
|
+
# Image and mask data generators
|
|
9
|
+
image_datagen = ImageDataGenerator(rescale=1./255)
|
|
10
|
+
mask_datagen = ImageDataGenerator(rescale=1./255)
|
|
11
|
+
|
|
12
|
+
image_generator = image_datagen.flow_from_directory(
|
|
13
|
+
image_dir,
|
|
14
|
+
class_mode=None,
|
|
15
|
+
color_mode='rgb',
|
|
16
|
+
target_size=(128, 128),
|
|
17
|
+
batch_size=32,
|
|
18
|
+
seed=42
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
mask_generator = mask_datagen.flow_from_directory(
|
|
22
|
+
mask_dir,
|
|
23
|
+
class_mode=None,
|
|
24
|
+
color_mode='grayscale',
|
|
25
|
+
target_size=(128, 128),
|
|
26
|
+
batch_size=32,
|
|
27
|
+
seed=42
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
# Combine generators into one which yields image and mask
|
|
31
|
+
train_generator = zip(image_generator, mask_generator)
|
|
32
|
+
|
|
33
|
+
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Conv2DTranspose, Input
|
|
34
|
+
from tensorflow.keras.models import Model
|
|
35
|
+
|
|
36
|
+
def build_fcnn():
|
|
37
|
+
inputs = Input((128, 128, 3))
|
|
38
|
+
|
|
39
|
+
# Encoder
|
|
40
|
+
conv1 = Conv2D(128, (3, 3), activation='relu', padding='same')(inputs)
|
|
41
|
+
pool1 = MaxPooling2D((2, 2))(conv1)
|
|
42
|
+
|
|
43
|
+
conv2 = Conv2D(256, (3, 3), activation='relu', padding='same')(pool1)
|
|
44
|
+
pool2 = MaxPooling2D((2, 2))(conv2)
|
|
45
|
+
|
|
46
|
+
# Decoder
|
|
47
|
+
conv3 = Conv2D(256, (3, 3), activation='relu', padding='same')(pool2)
|
|
48
|
+
up1 = Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(conv3)
|
|
49
|
+
|
|
50
|
+
conv4 = Conv2D(128, (3, 3), activation='relu', padding='same')(up1)
|
|
51
|
+
up2 = Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(conv4)
|
|
52
|
+
|
|
53
|
+
outputs = Conv2D(1, (1, 1), activation='sigmoid', padding='same')(up2)
|
|
54
|
+
|
|
55
|
+
model = Model(inputs, outputs)
|
|
56
|
+
return model
|
|
57
|
+
|
|
58
|
+
model = build_fcnn()
|
|
59
|
+
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
|
|
60
|
+
model.summary()
|
|
61
|
+
|
|
62
|
+
# Train the FCNN model
|
|
63
|
+
def combined_generator(image_gen, mask_gen):
|
|
64
|
+
while True: # Keep yielding data indefinitely
|
|
65
|
+
img_batch = next(image_gen)
|
|
66
|
+
mask_batch = next(mask_gen)
|
|
67
|
+
yield img_batch, mask_batch # Keras expects (input, target)
|
|
68
|
+
|
|
69
|
+
# Fit the model with the custom generator
|
|
70
|
+
train_generator = combined_generator(image_generator, mask_generator)
|
|
71
|
+
model.fit(train_generator, steps_per_epoch=len(image_generator), epochs=50)
|
|
72
|
+
|
|
73
|
+
import matplotlib.pyplot as plt
|
|
74
|
+
import numpy as np
|
|
75
|
+
|
|
76
|
+
# Sample image for prediction
|
|
77
|
+
sample_image = image_generator[0][0]
|
|
78
|
+
predicted_mask = model.predict(np.expand_dims(sample_image, axis=0))[0]
|
|
79
|
+
|
|
80
|
+
# Display the original image and predicted mask
|
|
81
|
+
plt.figure(figsize=(10, 5))
|
|
82
|
+
|
|
83
|
+
plt.subplot(1, 2, 1)
|
|
84
|
+
plt.title("Original Image")
|
|
85
|
+
plt.imshow(sample_image)
|
|
86
|
+
|
|
87
|
+
plt.subplot(1, 2, 2)
|
|
88
|
+
plt.title("Predicted Mask")
|
|
89
|
+
plt.imshow(predicted_mask.squeeze(), cmap='gray')
|
|
90
|
+
|
|
91
|
+
plt.show()
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# NOTE (original notebook line, keep as comment for .py use):
|
|
2
|
+
# !pip install --user torchvision
|
|
3
|
+
#pip install torch torchvision matplotlib requests pillow
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
import torch
|
|
7
|
+
import torchvision.transforms as T
|
|
8
|
+
from torchvision.models.detection import fasterrcnn_resnet50_fpn
|
|
9
|
+
from PIL import Image
|
|
10
|
+
import matplotlib.pyplot as plt
|
|
11
|
+
import numpy as np
|
|
12
|
+
|
|
13
|
+
model = fasterrcnn_resnet50_fpn(pretrained=True)
|
|
14
|
+
model.eval()
|
|
15
|
+
|
|
16
|
+
def load_image(image_path):
|
|
17
|
+
image = Image.open(image_path).convert("RGB")
|
|
18
|
+
transform = T.Compose([T.ToTensor()])
|
|
19
|
+
return transform(image).unsqueeze(0), image
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def detect_and_show(image_path):
|
|
23
|
+
image_tensor, original_image = load_image(image_path)
|
|
24
|
+
|
|
25
|
+
with torch.no_grad():
|
|
26
|
+
predictions = model(image_tensor)
|
|
27
|
+
|
|
28
|
+
pred_boxes = predictions[0]['boxes'].cpu().numpy()
|
|
29
|
+
pred_scores = predictions[0]['scores'].cpu().numpy()
|
|
30
|
+
|
|
31
|
+
plt.figure(figsize=(10, 10))
|
|
32
|
+
plt.imshow(original_image)
|
|
33
|
+
|
|
34
|
+
for i, (box, score) in enumerate(zip(pred_boxes, pred_scores)):
|
|
35
|
+
if score > 0.5:
|
|
36
|
+
x1, y1, x2, y2 = box
|
|
37
|
+
plt.gca().add_patch(plt.Rectangle((x1, y1), x2 - x1, y2 - y1, fill=False, edgecolor='red', linewidth=2))
|
|
38
|
+
plt.text(x1, y1, f'{score:.2f}', color='red', fontsize=12, weight='bold')
|
|
39
|
+
|
|
40
|
+
plt.axis('off')
|
|
41
|
+
plt.show()
|
|
42
|
+
|
|
43
|
+
# COCO dataset labels (91 classes, but model outputs 91 with background=0)
|
|
44
|
+
COCO_INSTANCE_CATEGORY_NAMES = [
|
|
45
|
+
'__background__', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
|
|
46
|
+
'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'N/A', 'stop sign',
|
|
47
|
+
'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
|
|
48
|
+
'elephant', 'bear', 'zebra', 'giraffe', 'N/A', 'backpack', 'umbrella',
|
|
49
|
+
'N/A', 'N/A', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard',
|
|
50
|
+
'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard',
|
|
51
|
+
'surfboard', 'tennis racket', 'bottle', 'N/A', 'wine glass', 'cup', 'fork',
|
|
52
|
+
'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange',
|
|
53
|
+
'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
|
|
54
|
+
'potted plant', 'bed', 'N/A', 'dining table', 'N/A', 'N/A', 'toilet',
|
|
55
|
+
'N/A', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
|
|
56
|
+
'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'N/A', 'book',
|
|
57
|
+
'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
def detect_and_show(image_path):
|
|
61
|
+
image_tensor, original_image = load_image(image_path)
|
|
62
|
+
|
|
63
|
+
with torch.no_grad():
|
|
64
|
+
predictions = model(image_tensor)
|
|
65
|
+
|
|
66
|
+
pred_boxes = predictions[0]['boxes'].cpu().numpy()
|
|
67
|
+
pred_scores = predictions[0]['scores'].cpu().numpy()
|
|
68
|
+
pred_classes = predictions[0]['labels'].cpu().numpy() # <-- class labels
|
|
69
|
+
|
|
70
|
+
plt.figure(figsize=(10, 10))
|
|
71
|
+
plt.imshow(original_image)
|
|
72
|
+
|
|
73
|
+
for i, (box, score, cls) in enumerate(zip(pred_boxes, pred_scores, pred_classes)):
|
|
74
|
+
if score > 0.5:
|
|
75
|
+
x1, y1, x2, y2 = box
|
|
76
|
+
label = COCO_INSTANCE_CATEGORY_NAMES[cls]
|
|
77
|
+
plt.gca().add_patch(plt.Rectangle((x1, y1), x2 - x1, y2 - y1,
|
|
78
|
+
fill=False, edgecolor='red', linewidth=2))
|
|
79
|
+
plt.text(x1, y1, f'{label}: {score:.2f}',
|
|
80
|
+
color='red', fontsize=12, weight='bold',
|
|
81
|
+
bbox=dict(facecolor='white', alpha=0.5, edgecolor='none'))
|
|
82
|
+
|
|
83
|
+
plt.axis('off')
|
|
84
|
+
plt.show()
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
image_path = "/content/apple_4.jpg"
|
|
89
|
+
detect_and_show(image_path)
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import numpy as np
|
|
3
|
+
import pandas as pd
|
|
4
|
+
import tensorflow as tf
|
|
5
|
+
import matplotlib.pyplot as plt
|
|
6
|
+
import sklearn.preprocessing
|
|
7
|
+
from sklearn.metrics import r2_score
|
|
8
|
+
|
|
9
|
+
from keras.layers import Dense,Dropout,SimpleRNN,LSTM
|
|
10
|
+
from keras.models import Sequential
|
|
11
|
+
|
|
12
|
+
#check all the files in the input dataset
|
|
13
|
+
#print(os.listdir("../input/"))
|
|
14
|
+
|
|
15
|
+
#choosing DOM_hourly.csv data for analysis
|
|
16
|
+
fpath='D://Users//user//Downloads//DOM_hourly.csv//DOM_hourly.csv'
|
|
17
|
+
|
|
18
|
+
df=pd.read_csv(fpath)
|
|
19
|
+
df.head()
|
|
20
|
+
|
|
21
|
+
#Let's use datetime(2012-10-01 12:00:00,...) as index instead of numbers(0,1,...)
|
|
22
|
+
#This will be helpful for further data analysis as we are dealing with time series data
|
|
23
|
+
df = pd.read_csv(fpath, index_col='Datetime', parse_dates=['Datetime'])
|
|
24
|
+
df.head()
|
|
25
|
+
|
|
26
|
+
#checking missing data
|
|
27
|
+
df.isna().sum()
|
|
28
|
+
|
|
29
|
+
df.plot(figsize=(16,4),legend=True)
|
|
30
|
+
|
|
31
|
+
plt.title('DOM hourly power consumption data - BEFORE NORMALIZATION')
|
|
32
|
+
|
|
33
|
+
plt.show()
|
|
34
|
+
|
|
35
|
+
def normalize_data(df):
|
|
36
|
+
scaler = sklearn.preprocessing.MinMaxScaler()
|
|
37
|
+
df['DOM_MW']=scaler.fit_transform(df['DOM_MW'].values.reshape(-1,1))
|
|
38
|
+
return df
|
|
39
|
+
|
|
40
|
+
df_norm = normalize_data(df)
|
|
41
|
+
df_norm.shape
|
|
42
|
+
|
|
43
|
+
df_norm.plot(figsize=(16,4),legend=True)
|
|
44
|
+
|
|
45
|
+
plt.title('DOM hourly power consumption data - AFTER NORMALIZATION')
|
|
46
|
+
|
|
47
|
+
plt.show()
|
|
48
|
+
|
|
49
|
+
df_norm.shape
|
|
50
|
+
|
|
51
|
+
def load_data(stock, seq_len):
|
|
52
|
+
X_train = []
|
|
53
|
+
y_train = []
|
|
54
|
+
for i in range(seq_len, len(stock)):
|
|
55
|
+
X_train.append(stock.iloc[i-seq_len : i, 0])
|
|
56
|
+
y_train.append(stock.iloc[i, 0])
|
|
57
|
+
|
|
58
|
+
#1 last 6189 days are going to be used in test
|
|
59
|
+
X_test = X_train[110000:]
|
|
60
|
+
y_test = y_train[110000:]
|
|
61
|
+
|
|
62
|
+
#2 first 110000 days are going to be used in training
|
|
63
|
+
X_train = X_train[:110000]
|
|
64
|
+
y_train = y_train[:110000]
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
#3 convert to numpy array
|
|
68
|
+
X_train = np.array(X_train)
|
|
69
|
+
y_train = np.array(y_train)
|
|
70
|
+
print(X_train.shape)
|
|
71
|
+
X_test = np.array(X_test)
|
|
72
|
+
y_test = np.array(y_test)
|
|
73
|
+
|
|
74
|
+
#4 reshape data to input into RNN models
|
|
75
|
+
X_train = np.reshape(X_train, (110000, seq_len, 1))
|
|
76
|
+
print(X_train.shape)
|
|
77
|
+
|
|
78
|
+
X_test = np.reshape(X_test, (X_test.shape[0], seq_len, 1))
|
|
79
|
+
|
|
80
|
+
return [X_train, y_train, X_test, y_test]
|
|
81
|
+
|
|
82
|
+
#create train, test data
|
|
83
|
+
seq_len = 20 #choose sequence length
|
|
84
|
+
|
|
85
|
+
X_train, y_train, X_test, y_test = load_data(df, seq_len)
|
|
86
|
+
|
|
87
|
+
print('X_train.shape = ',X_train.shape)
|
|
88
|
+
print('y_train.shape = ', y_train.shape)
|
|
89
|
+
print('X_test.shape = ', X_test.shape)
|
|
90
|
+
print('y_test.shape = ',y_test.shape)
|
|
91
|
+
|
|
92
|
+
rnn_model = Sequential()
|
|
93
|
+
|
|
94
|
+
rnn_model.add(SimpleRNN(40,activation="tanh",return_sequences=True, input_shape=(X_train.shape[1],1)))
|
|
95
|
+
rnn_model.add(Dropout(0.15))
|
|
96
|
+
|
|
97
|
+
rnn_model.add(SimpleRNN(40,activation="tanh",return_sequences=True))
|
|
98
|
+
rnn_model.add(Dropout(0.15))
|
|
99
|
+
|
|
100
|
+
rnn_model.add(SimpleRNN(40,activation="tanh",return_sequences=False))
|
|
101
|
+
rnn_model.add(Dropout(0.15))
|
|
102
|
+
|
|
103
|
+
rnn_model.add(Dense(1))
|
|
104
|
+
|
|
105
|
+
rnn_model.summary()
|
|
106
|
+
|
|
107
|
+
print(input_shape)
|
|
108
|
+
|
|
109
|
+
rnn_model.compile(optimizer="adam",loss="MSE")
|
|
110
|
+
rnn_model.fit(X_train, y_train, epochs=10, batch_size=1000)
|
|
111
|
+
|
|
112
|
+
rnn_predictions = rnn_model.predict(X_test)
|
|
113
|
+
|
|
114
|
+
rnn_score = r2_score(y_test,rnn_predictions)
|
|
115
|
+
print("R2 Score of RNN model = ",rnn_score)
|
|
116
|
+
|
|
117
|
+
def plot_predictions(test, predicted, title):
|
|
118
|
+
plt.figure(figsize=(16,4))
|
|
119
|
+
plt.plot(test, color='blue',label='Actual power consumption data')
|
|
120
|
+
plt.plot(predicted, alpha=0.7, color='orange',label='Predicted power consumption data')
|
|
121
|
+
plt.title(title)
|
|
122
|
+
plt.xlabel('Time')
|
|
123
|
+
plt.ylabel('Normalized power consumption scale')
|
|
124
|
+
plt.legend()
|
|
125
|
+
plt.show()
|
|
126
|
+
|
|
127
|
+
plot_predictions(y_test, rnn_predictions, "Predictions made by simple RNN model")
|
|
128
|
+
|
|
129
|
+
lstm_model = Sequential()
|
|
130
|
+
|
|
131
|
+
lstm_model.add(LSTM(40,activation="tanh",return_sequences=True, input_shape=(X_train.shape[1],1)))
|
|
132
|
+
lstm_model.add(Dropout(0.15))
|
|
133
|
+
|
|
134
|
+
lstm_model.add(LSTM(40,activation="tanh",return_sequences=True))
|
|
135
|
+
lstm_model.add(Dropout(0.15))
|
|
136
|
+
|
|
137
|
+
lstm_model.add(LSTM(40,activation="tanh",return_sequences=False))
|
|
138
|
+
lstm_model.add(Dropout(0.15))
|
|
139
|
+
|
|
140
|
+
lstm_model.add(Dense(1))
|
|
141
|
+
|
|
142
|
+
lstm_model.summary()
|
|
143
|
+
|
|
144
|
+
lstm_model.compile(optimizer="adam",loss="MSE")
|
|
145
|
+
lstm_model.fit(X_train, y_train, epochs=10, batch_size=1000)
|
|
146
|
+
|
|
147
|
+
lstm_predictions = lstm_model.predict(X_test)
|
|
148
|
+
|
|
149
|
+
lstm_score = r2_score(y_test, lstm_predictions)
|
|
150
|
+
print("R^2 Score of LSTM model = ",lstm_score)
|
|
151
|
+
|
|
152
|
+
plot_predictions(y_test, lstm_predictions, "Predictions made by LSTM model")
|
|
153
|
+
|
|
154
|
+
plt.figure(figsize=(15,8))
|
|
155
|
+
|
|
156
|
+
plt.plot(y_test, c="orange", linewidth=3, label="Original values")
|
|
157
|
+
plt.plot(lstm_predictions, c="red", linewidth=3, label="LSTM predictions")
|
|
158
|
+
plt.plot(rnn_predictions, alpha=0.5, c="green", linewidth=3, label="RNN predictions")
|
|
159
|
+
plt.legend()
|
|
160
|
+
plt.title("Predictions vs actual data", fontsize=20)
|
|
161
|
+
plt.show()
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: maanbumigu
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Personal code snippet library - retrieve saved source code via mb.get()
|
|
5
|
+
Author: arunkumar
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: snippets,tensorflow,pytorch,dnn,cnn
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Requires-Python: >=3.9
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# maanbumigu
|
|
14
|
+
|
|
15
|
+
Personal snippet library. Keeps your source code as-is and retrieves it with one line:
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
import maanbumigu as mb
|
|
19
|
+
|
|
20
|
+
mb.list() # ['CNN', 'DNN', 'FCNN', 'FRCNN_RESNET', 'RNN_LSTM']
|
|
21
|
+
mb.get("DNN") # prints + returns DNN code
|
|
22
|
+
mb.get("CNN")
|
|
23
|
+
mb.get("FCNN")
|
|
24
|
+
mb.get("FRCNN") # alias for FRCNN_RESNET
|
|
25
|
+
mb.get("RNN") # alias for RNN_LSTM
|
|
26
|
+
mb.get("LSTM") # alias for RNN_LSTM
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Save to file:
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
mb.get("DNN", save_as="dnn_run.py")
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Publish to PyPI (maintainer)
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
cd /Users/arunkumar/maanbumigu
|
|
39
|
+
python3 -m pip install --upgrade build twine
|
|
40
|
+
python3 -m build
|
|
41
|
+
python3 -m twine upload dist/*
|
|
42
|
+
# enter your PyPI username + password / API token when asked
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Then anyone can install:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
pip install maanbumigu
|
|
49
|
+
```
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
src/maanbumigu/__init__.py
|
|
4
|
+
src/maanbumigu.egg-info/PKG-INFO
|
|
5
|
+
src/maanbumigu.egg-info/SOURCES.txt
|
|
6
|
+
src/maanbumigu.egg-info/dependency_links.txt
|
|
7
|
+
src/maanbumigu.egg-info/top_level.txt
|
|
8
|
+
src/maanbumigu/snippets/__init__.py
|
|
9
|
+
src/maanbumigu/snippets/cnn.py
|
|
10
|
+
src/maanbumigu/snippets/dnn.py
|
|
11
|
+
src/maanbumigu/snippets/fcnn.py
|
|
12
|
+
src/maanbumigu/snippets/frcnn_resnet.py
|
|
13
|
+
src/maanbumigu/snippets/rnn_lstm.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
maanbumigu
|