MCPcopy Create free account
hub / github.com/GitsSaikat/PyGen / train

Method train

data/AutoVison/context_code.py:50–80  ·  view source on GitHub ↗

Train the model with extracted features

(self, images, labels, test_size=0.2)

Source from the content-addressed store, hash-verified

48 return extract_features(img)
49
50 def train(self, images, labels, test_size=0.2):
51 """Train the model with extracted features"""
52 features = []
53 for img in images:
54 processed_img = self.preprocess_image(img)
55 img_features = self.extract_features(processed_img)
56 # Concatenate all feature types
57 feature_vector = np.concatenate([
58 img_features['hog'].flatten(),
59 img_features['color'].flatten(),
60 img_features['sift'].flatten() if img_features['sift'] is not None else np.zeros(128)
61 ])
62 features.append(feature_vector)
63
64 X_train, X_test, y_train, y_test = train_test_split(
65 features, labels, test_size=test_size, random_state=42
66 )
67
68 self.model = RandomForestClassifier(n_estimators=100, random_state=42)
69 self.model.fit(X_train, y_train)
70
71 # Evaluate model
72 y_pred = self.model.predict(X_test)
73 accuracy = accuracy_score(y_test, y_pred)
74 report = classification_report(y_test, y_pred)
75
76 return {
77 'accuracy': accuracy,
78 'report': report,
79 'model': self.model
80 }
81
82 def predict(self, img):
83 """Predict class for new image"""

Callers

nothing calls this directly

Calls 3

preprocess_imageMethod · 0.95
extract_featuresMethod · 0.95
predictMethod · 0.45

Tested by

no test coverage detected