Skip to content

migration#16

Merged
jamilgafur merged 1 commit intomainfrom
Poison
Apr 8, 2026
Merged

migration#16
jamilgafur merged 1 commit intomainfrom
Poison

Conversation

@jamilgafur
Copy link
Copy Markdown
Collaborator

1. Adversarial Attacks (Attacks.py)

  • Signature & Logic Simplifications:
    • gradient_map: Replaced complex backpropagation options (guided, relu) with a simpler implementation utilizing a new compute_gradients helper function.
    • gradient_ascent: Significantly altered the signature. It now maximizes a target_class on a single input image rather than maximizing a specific target_neuron across a batch of data.
    • fgsm_attack: Modified to return a list of tensors rather than a stacked tensor array (torch.stack).
  • Code Cleanup: Removed several unused utility functions, including visualize_adversarial_examples, log_metrics, saliency_map, and the Config class.

2. Particle Logic (BirdParticle.py)

  • Model Decoupling: The BirdParticle initialization no longer requires PyTorch-specific arguments like model, input_data, target_class, or device. It now only tracks its own position, velocity, and clamping boundaries.
  • Method Refactoring: * The fitness() method was replaced by evaluate(costFunc, model), delegating the evaluation logic to an external cost function rather than calculating softmax probabilities internally.
    • update_velocity now takes a simple list/tensor for the global best position (pos_best_g) instead of recalculating based on the model state.
    • Clamping constraints were simplified to use minclamp and maxclamp parameters instead of clip_value_position.

3. Swarm Logic (Swarm.py)

  • Class Renaming: ParticleSwarm was renamed to PSO.
  • Core Architectural Shift: * Like BirdParticle, the swarm initialization was heavily stripped down. It now accepts starting_positions and an explicit cost_func rather than hardcoding image classification concepts like starting_class and target_class.
    • The internal execution loop (run() with tqdm progress bars) and internal logging (setup_logging, log_progress) were removed. The swarm now relies on a step() function, giving the user control over the training loop.
  • Data Handling & Output: * Added a get_history() method that outputs the swarm's positional history as a pandas.DataFrame, and a save_history() method to export it to a CSV.
    • getBest() and getPoints() were updated to return torch.Tensor objects rather than unhooked NumPy arrays.
    • The reduce_excess_perturbations method was completely removed.

4. Unit Tests (tests/)

  • test_adversarial_attacks.py: Updated assertions to account for the attacks returning lists instead of single multi-dimensional tensors, and updated test_gradient_ascent to pass target_class instead of target_neuron.
  • test_bird_particle.py: Removed the PyTorch model fixture dependency, as BirdParticle no longer requires it for initialization. Updated parameter names to match minclamp and maxclamp.
  • test_pso_optimization.py: * Updated references from ParticleSwarm to PSO.
    • Implemented a dummy_cost_func fixture to fulfill the new PSO initialization requirements.
    • Updated type-checking assertions, as extraction methods now return torch.Tensor instead of np.ndarray.
    • Updated the logging test to verify the creation of the new pandas-driven history.csv file rather than a standard .log file.

@jamilgafur jamilgafur merged commit 8c1d553 into main Apr 8, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant