Skip to content

moldluca/TrainBot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TrainBot

TrainBot is an iOS application that lets users teach and test a machine learning model through interactive and engaging activities. The app features multiple integrated modules:

  • Training: Users can upload and label images in the TrainingView to build a knowledge base. For example, a user might upload pictures of cats and label them as "Cat." The app uses a built-in image classifier (ImageClassifier) to either suggest labels (e.g., "Is this a Cat?") or learn from new examples. This process helps the model improve its accuracy over time.

    // Example: Adding a new labeled image to the training set
    let image = UIImage(named: "cat.jpg")
    let label = "Cat"
    ImageClassifier.shared.addTrainingExample(image: image, label: label)
    print("Training example added: \(label)")
  • Testing: In the TestingView, users select an image to see how well the model can recognize and classify it. For instance, if the user uploads a new picture of a cat, the app will display the predicted label (e.g., "Cat") along with a confidence score (e.g., 95%). Feedback is provided via on-screen results and sound effects managed by SoundManager, such as a "success" sound for correct predictions.

    // Example: Testing an image with the classifier
    let testImage = UIImage(named: "test_cat.jpg")
    let result = ImageClassifier.shared.predict(image: testImage)
    print("Predicted label: \(result.label), Confidence: \(result.confidence)%")
    SoundManager.shared.playFeedback(for: result.isCorrect)
  • Knowledge Base: The KnowledgeView displays previously stored images and their labels. Users can browse their labeled images, delete specific images, or remove entire labels. For example, if a user no longer wants to keep the "Dog" label, they can delete it along with all associated images. Images are saved and loaded using StorageManager, ensuring persistence across app sessions.

    // Example: Deleting a label and its associated images
    let labelToDelete = "Dog"
    StorageManager.shared.deleteLabel(labelToDelete)
    print("Label '\(labelToDelete)' and its images have been deleted.")
  • Daily Challenges: Each day, a unique challenge is generated by DailyChallengeManager. For example, a challenge might ask users to upload three images of "Red Objects" or classify five images of "Animals." Completing these tasks earns bonus points, encouraging users to engage with the app regularly.

    // Example: Generating and completing a daily challenge
    let challenge = DailyChallengeManager.shared.generateChallenge()
    print("Today's challenge: \(challenge.description)")
    DailyChallengeManager.shared.completeChallenge(challenge)
    print("Challenge completed! Bonus points awarded.")
  • Achievements: As users progress, they unlock achievements displayed in the AchievementsView. For instance, an achievement might be "First 10 Images Labeled" or "100% Accuracy on a Test." These achievements are managed through AchievementsManager, which saves progress and triggers celebratory notifications, such as a confetti animation or a congratulatory message.

    // Example: Unlocking an achievement
    let achievement = "First 10 Images Labeled"
    AchievementsManager.shared.unlockAchievement(achievement)
    print("Achievement unlocked: \(achievement)")
  • Settings: Through the SettingsView, users can customize app settings. For example, they can change the bot's name from "TrainBot" to "BuddyBot" or toggle sound effects on/off. These preferences are stored in AppSettings and persist across app launches.

    // Example: Updating app settings
    AppSettings.shared.botName = "BuddyBot"
    AppSettings.shared.soundEffectsEnabled = false
    print("Settings updated: Bot name is now \(AppSettings.shared.botName)")

The app also integrates iOS notifications via NotificationManager to remind users to train their bot at different periods of the day. For example, a notification might say, "Don't forget to train your bot today!" at 6 PM.

// Example: Scheduling a notification
NotificationManager.shared.scheduleNotification(
    title: "TrainBot Reminder",
    body: "Don't forget to train your bot today!",
    time: Date().addingTimeInterval(3600) // 1 hour from now
)
print("Notification scheduled.")

The application's entry point is defined in Main.swift, which initializes the app and sets up the main navigation flow. The onboarding experience is provided in OnboardingView, guiding new users through the app's features. For example, the onboarding might include a tutorial on how to upload and label images or how to complete a daily challenge.

// Example: Starting the onboarding process
let onboardingView = OnboardingView()
onboardingView.startTutorial()
print("Onboarding started.")

TrainBot combines machine learning, gamification, and daily challenges to create a fun learning environment where users can continuously improve the bot's image recognition skills.

How the Machine Learning Model is Trained and Tested

The core of TrainBot's functionality revolves around its machine learning model, which is trained to classify images based on user-provided labels. Below is a detailed explanation of how the training and testing processes work, along with the relevant code.

Training the Model

The training process involves adding labeled images to the model's dataset. Each image is associated with a label (e.g., "Cat" or "Dog"), and the model uses this data to learn patterns and improve its classification accuracy.

// Example: Training the model with labeled images
let image = UIImage(named: "cat.jpg") // Load an image
let label = "Cat" // Define the label for the image

// Add the image and label to the training dataset
ImageClassifier.shared.addTrainingExample(image: image, label: label)

// Train the model with the updated dataset
ImageClassifier.shared.train { success in
    if success {
        print("Model training completed successfully!")
    } else {
        print("Model training failed.")
    }
}

Explanation:

  1. UIImage(named: "cat.jpg"): Loads an image from the app's resources.
  2. addTrainingExample(image:label:): Adds the image and its label to the training dataset.
  3. train { success in ... }: Trains the model asynchronously and provides a callback to indicate whether the training was successful.

Testing the Model

Once the model is trained, users can test it by providing new images. The model predicts the label for the image and provides a confidence score.

// Example: Testing the model with a new image
let testImage = UIImage(named: "test_dog.jpg") // Load a test image

// Predict the label for the test image
let result = ImageClassifier.shared.predict(image: testImage)

// Display the prediction results
print("Predicted label: \(result.label)")
print("Confidence: \(result.confidence)%")

// Provide feedback based on the prediction's correctness
if result.isCorrect {
    SoundManager.shared.playFeedback(for: true) // Play success sound
} else {
    SoundManager.shared.playFeedback(for: false) // Play failure sound
}

Explanation:

  1. UIImage(named: "test_dog.jpg"): Loads a new image for testing.
  2. predict(image:): Uses the trained model to predict the label for the image. Returns a result object containing:
    • label: The predicted label (e.g., "Dog").
    • confidence: The confidence score (e.g., 92%).
    • isCorrect: A boolean indicating whether the prediction matches the expected label.
  3. SoundManager.shared.playFeedback(for:): Plays a sound effect based on whether the prediction was correct.

Behind the Scenes: How the Model Works

The ImageClassifier class is the core component responsible for training and testing the model. Here's a high-level overview of its implementation:

// Example: Simplified ImageClassifier implementation
class ImageClassifier {
    static let shared = ImageClassifier()
    private var trainingData: [(UIImage, String)] = [] // Stores images and labels
    private var model: MLModel? // The machine learning model

    // Add a training example
    func addTrainingExample(image: UIImage, label: String) {
        trainingData.append((image, label))
    }

    // Train the model
    func train(completion: (Bool) -> Void) {
        // Convert training data into a format suitable for the ML model
        let mlData = prepareMLData(from: trainingData)
        do {
            model = try MLModel.train(with: mlData) // Train the model
            completion(true)
        } catch {
            print("Error training model: \(error)")
            completion(false)
        }
    }

    // Predict the label for a new image
    func predict(image: UIImage) -> (label: String, confidence: Double, isCorrect: Bool) {
        guard let model = model else {
            return ("Unknown", 0.0, false)
        }
        let prediction = model.predict(image: image) // Perform prediction
        return (prediction.label, prediction.confidence, prediction.isCorrect)
    }

    // Helper function to prepare ML data
    private func prepareMLData(from data: [(UIImage, String)]) -> MLData {
        // Convert images and labels into a format the ML model can use
        // ...implementation details...
    }
}

Key Points:

  1. trainingData: Stores the images and their labels.
  2. train(completion:): Converts the training data into a format suitable for the ML model and trains it.
  3. predict(image:): Uses the trained model to predict the label for a new image.

By combining user-provided data with machine learning, TrainBot creates a personalized and interactive experience where users can actively improve the model's performance over time.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages